Wednesday, August 29, 2007

Process of different accesses

class C{
virtual void proc (int x) {...}
..
};
..
C obj;
C* ptr;
..
obj.proc(5);
ptr->proc(5);

For obj.proc(5):
1. calculate the address of obj, and push this address onto the stack (or load it into a registry). Member function requires "this" pointer.
2. push 5 onto the stack (or load it into a registry).
3. call C::proc(int). Assuming compiler is smart enough to de-virtual the call to proc.

For ptr->proc(5):
1. push the value of ptr onto the stack (or load it into a registry).
2. push 5 onto the stack (or load it into a registry).
3. get the entry address from vtable.
4. call C::proc using indirect address.

No comments:

Post a Comment