Polymorphism is one of the most important features of OOP. C++ implements polymorphism by the combination of virtual member functions and pointers (references). As a C++ user, of course you know how to use virtual member function and pointers (references) to achive polymorphism. But there might be a fuzzy zone left around these concepts. For example, have you ever think about the following question?
Can a pure virtual function has a implementation?
This post discuss declaration and definition of virtual member functions in C++.
Short Answer to the Question
Yes, we could give a definition of a pure virtual function, and sometimes we must give such a definition. But remember, unlike other member functions, the definition of a pure virtual member function should be implemented outside the definition of the class contains it. (see the example below)
1 | struct Abstract { |
Virtual Member Functions
Here, we begin to discuss the rules about declaration and definition of virtual member functions. According to the C++ standard, a virtual member function declared in the definition of a class must have a definition (implementation). Remember that, when you are trying to give implementation of a virtual member function, you cannot mark virtual
again outside the class definition.
1 | struct Foo { |
However, this rule is not required to give a diagnose when compiling. That is to say, if the definition of a virtual member function is missing, your compiler may not report an error. However, your linker might report it for referancing undefined symbol.
Pure Virtual Member Functions
Pure virtual functions mark classes who contain them as "abstract class". It means that no objects of an abstract calss could be created, and we cannot use it as parameter types, as function return types, or as the type of an explicit conversion.
As a result, we'll never have a chance to call virtual member functions declared in an abstract class from its object. On the other hand, since it's a pure virtual function and will defintely be overrided in dirved classes. In consquence, in most cases, impl of pure virtual function is useless. Therefore, we can declare a function as pure function and give no definition of it.
That is to say, for pure virtual member function, we could:
- declare it in class definition, and give no implementation of it;
- declare it in class definition, and do give implementation of it outside the class definition.
But remember, there are two exceptions.
- for pure virtual destructor, you'll have to provide a definition.
- the member functions of the derived class are free to call the abstract base's pure virtual function using qualified function id (
Base::some_pure_virtual_function()
).
In these two cases, definition of pure virtual function makes sense.