Pure virtual function in C++: Is it possible to define a Pure Virtual Function in C++?
I am not surprised if you are surprised after reading the title because the title is contradicting the definition of pure virtual function.
I assume that you have a basic idea of what virtual function is and why and how we use it.
Pure Virtual function:
It is a virtual function that is provided in a class by assigning 0 (= 0 is known as pure-specifier) to the function which makes that class an abstract class and makes it compulsory for the non-abstract derived class to provide a definition to this pure virtual function.
virtual void function() = 0;
What happens when you declare a pure virtual function in a class?
When you declare a pure virtual function in a class,
that class becomes an abstract class. You can not create an instance of an abstract class.
If you want to use this class, you will have to inherit this class to your derived class and because it is a pure virtual function, it becomes compulsory for the derived class to implement this.
So what happens behind the scene?
When you declare a virtual function,
the compiler creates a VTABLE for the class and adds an entry of your virtual function in that table.
VTABLE contains the function pointer of the virtual function.
For pure virtual function, this entry is null. This is the reason we can not create an instance of an abstract class.
Pure virtual functions in a class usually don't have a definition/implementation.
Hey wait, is the title of this blog is wrong?
"No, absolutely not."
Let's see some code. Code will explain this concept very clearly.
from derived class
This is a typical condition and we use pure virtual functions like this most of the time.
Now, let's give a definition of our pure virtual function.
from base class from derived class
If you compile and run this code this will work absolutely fine.
Let me give you some pointers.
You can not define your pure virtual function inside the class.
You can define your function outside your class with scope resolution. Same as you define a normal member function.
Now the important point is, this function definition will not get called implicitly. You will have to call this function explicitly using the class name.
In derived class fun() definition, if you remove call to base::fun(). This function will not get called implicitly.
But what and where is the use of this feature?
This is a very important section and if I don't talk about this, there is no point in discussing this topic because you will not use this feature most of the time. If you don't know about this, this may almost never impact you.
But there may be situations, where you may need this feature.
Let's assume, You have an interface class that has a pure virtual function to handle errors.
virtual void handle_errors(error_code) = 0;
Now by making this function pure virtual, you are forcing derived classes to implement their own version of `void handle_errors(error_code)`. Now think, if you want to provide a default error handling mechanism. In this situation, you have 2 options(there may be more, I don't know)
You define another function for default behavior and call to that function in your derived class.
Or you give a definition to your pure virtual function and call in your derived class implementation explicitly.
Another situation can be that for some reason you are not finding any correct function to make a pure virtual function. In that case, you can make a destructor of the base class as a pure virtual destructor and give it a definition.
If you want me to write on pure virtual destructor, let me know.
Hope this concept is clear and let me know if you have any queries. See you in my next blog.
Effective C++ by Scott Meyers