hi,
vtable is an overhead in all base/derived classes when a base class has a virtual function..vtable is supposed to contain and array of function pointers to these virtual functions..also vtable is "one per class" as opposed to "one per object"...now imagine an object of such a class being created..it'll get a fresh copy of virtual functions of the class in some memory location by the runtime..since vtable is a collection of function pointers it'll be updated to reflect this...if another object of the same class is created then it'll again have a fresh copy of the virtual functions in some other memory location since vtable is "one per class" and not "one per instance" how will it point to correct location for different instances?
As per my understanding ... the pointer of the function are created as per the object of the derived class and the virtual pointer will point to the function of the respective object ..the object variable are created as per respective object .. while calling the function the virtual pointer will point the pointer of the respective function of the object through the virtual pointer .
compiler defines virtual pointer only for the base class (else base class pointer wont be able to see it and hence no dynamic polymorphism)..this virtual pointer points to the entries in the vtable...since derived classes have access to the base class members they will update this virtual pointer to point to "their" vtable so that the base class pointer can reference the derived class virtual functions by this trick...but vtable is one per class... my question is if you have multiple objects of the same class (in a hierarchy), how will the virtual pointer pointing to a single virtual table refer to the correct addresses of the virtual functions for these different objects?
if you have multiple objects of the same class (in a hierarchy), how will the virtual pointer pointing to a single virtual table refer to the correct addresses of the virtual functions for these different objects
Still i am not able to get you clearly ..please can you explain me with the help of the example..so that i will understand what you mean to say .
now imagine an object of such a class being created..it'll get a fresh copy of virtual functions of the class in some memory location by the runtime..since vtable is a collection of function pointers it'll be updated to reflect this.
There is no such thing as "a fresh copy of virtual functions". Each function exists as a chunk of machine code at a fixed address in the code segment (the ".text" section). Executable code is never copied at runtime.
When a Derived object is created at run time, its vptr is initialized with the address of Derived::vtbl, exactly the same in all objects of Derived type.
(granted, nothing in C++ requires the use of virtual function tables and pointers to them in the first place, it's just the solution chosen by most compiler vendors)
There is no such thing as "a fresh copy of virtual functions". Each function exists as a chunk of machine code at a fixed address in the code segment (the ".text" section). Executable code is never copied at runtime.