I do have some doubts and i need help from you.

When we call a function which is virtual from a base class pointer to which the address of derived class object, it sees where vpointer, from vpointer it gets the address fo virtual table, manipulate offset and call the right function from vtable.
My question is, when does this vtable get populated ? As the vtable contain the address of all the virtual function belonging to the particular class, does memory is being allocated to the functions of that class before they are being called?

could anyone give a clear description of memory allocation process when virtualism works.

Recommended Answers

All 2 Replies

>could anyone give a clear description of memory allocation process when virtualism works.
Obviously this varies from compiler to compiler, but a typical implementation will generate vtables at build time. So when your program is loaded into memory, the vtables are already set up for the inheritance hierarchy. The constructor of new objects will then set the vtable pointer appropriately for the actual type.

Hi,
1. When a function is made virtual, C++ determines which function to be used at runtime based on the type of object pointed to by the base pointer,rather than the type of the pointer.

2. By making the base pointer to poin to the different objects , we can execute different versions of the virtual function.

3.If a virtual function is defined in the base class it need not be necessarly redefined in the derived class. In such cases , the respective calls will invoke the base class function.


This is covered in object oriented programming concepts[ <<plug removed>> go through.

Regards
Kolla Sanjeeva Rao

commented: Completely ignores the question, presumably to spam a tutorial. -4
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.