Consider the following C++ function:
void doStuff() {
Thing thingA;
Thing thingB;
thingA.doSomething();
// .. etc
}
During the execution of this function, variables thingA
and thingB
are allocated on the stack. That means that the variable thingA
represents some address on the stack, and it's value is what's allocated in this address (correct me if I'm wrong).
What I don't understand is this: when we call thingA.doSomething()
, how does the CPU know what the address represented by thingA
is? The value held by thingA
is currently buried in the stack under some other data. How does the CPU know what the address that leads to this value is, in order to reach this data?
thingA
is and where the functions are for aThing
. I've never worked much with C++ (although I've done embedded assembly work), so I don't know all the details. Just remember that modern CPUs are incredibly complex, but at their essence, they're dumb machines. They do exactly what you tell them to do, nothing more.