class C{ virtual void proc (int x) {...}..}; .. C obj; C* ptr; .. obj.proc(5); //(1) ptr->proc(5); //(2) 请问1和2的却别,2要比1多执行几步机器指令?
g*n
2 楼
For obj.proc(5): 1. calculat the address of obj, and push this address onto the stack (or load it into a registary). Member function requires "this" pointer. 2. push 5 onto the stack (or load it into a registary). 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 registary). 2. push 5 onto the stack (or load it into a registary). 3. get the enry address from vtable. 4. call C::proc