![]() |
Virtual Functions in C++ Part II
In part I of the article about Virtual Functions, we learnt about the virtual functions, why are they needed and when to use them. This article will talk about virtual function internals and how virtual functions work in C++. Actually, virtual functions govern to an extent, how a class object is laid in the memory. We shall be learning more in further sections.
Looking back at the BehaviorLet us brush up the behavior of virtual functions, which is very essential to understand before we jump onto its working. We have a base class and a derived class. The base class has a virtual function defined, and the derived class has the same function definition overridden. If we have a base class pointer, there are two possible cases.
How virtual functions work?When a class has virtual function or virtual functions, the particular class will hold a virtual table, also called the v-table. Moreover, compiler adds a pointer, called a virtual pointer, to this v-table in the memory allocation. This would be hidden and not directly accessible though. What does this v-table stores? Well, it stores the addresses of the virtual functions in the base class. Not only the base class, even the derived classes with virtual functions in their parent class, also hold a v-table. In the derived classes, if the derived class overrides the of virtual class of the parent class, then v-table contains the address of the function defined in the derived class. In case, the derived class does not override the virtual function of the parent class, derived class v-table contains the address of the virtual function definition of the parent class. This is how, dynamic polymorphism works in C++ using virtual tables. Virtual DestructorsA destructor of the base class needs to be declared as virtual. Even the virtual destructors have the same behavior as that of virtual functions. However, it has an additional feature. When a derived class object goes out of scope, the destructor of the derived class is called followed by the call to the base class destructor. Hence, combining both the features in circumstances where we have a base class pointer, pointing to a derived class. Now, when this pointer goes out of scope, it would call the base class destructor only, which would lead to a discrepancy. This discrepancy can be resolved by declaring the base class destructor as virtual. And it also explains the reason as to why is it needed. Let us see the same circumstance in implementation. Code:
Code:
ubuntu@ubuntu-VirtualBox:~$ g++ main.cpp -o mainWe resolved this problem by declaring the base destructor as virtual as follows Code:
Code:
ubuntu@ubuntu-VirtualBox:~$ g++ main.cpp -o main |
| All times are GMT +5.5. The time now is 11:36. |