More about Virtual Functions
More about virtual
virtual
Now we already learn the ability of virtual
. But is there any function that cannot be implemented as virtual function? Notice that a virtual function must meet two conditions:
It has an address and can be stored in the virtual function table.
Its existence depends on the object.
Obviously, the constructor can not be virtual, since no object exists before its constructor is called. In this case, any function called in the constructor is a static binding. Moreover, a static member function can not be virtual, since it belongs to the class itself instead of its objects.
Is there any function that should always be virtual? Let's look at the following case:
Now we use a Base pointer to point to a Derived object, and delete it after then.
If we look at the output, we may find that only the destructor for class Base is called. The destructor of class Derived is not called, which will probably cause memory leak.
In the example above, the destructor of the base class is not marked as virtual
, so there is a static binding. Good programming practice is to always marked the destructor of a base class as virtual
, in which case the destructor of the derived class becomes virtual automatically.
More about Dynamic Binding
There is another question: is the call of a virtual function is always a dynamic binding? We already know that any function call in a constructor is static binding, even if the constructor is marked as virtual
. Let's look at more examples.
Here we create two objects, and call the show() method directly with the object. In this case, the function calls are static binding.
Now if we use a Base pointer to point to objects, it is a dynamic binding, no matter the object we point at is a Base object or a Derived object.
References have the same underlying implementation as pointers, so it is also a dynamic binding.
Now we know that dynamic binding is a way to achieve polymorphism. Therefore, only with pointers and references is the function call a dynamic binding.
Last updated