Providing an implementation of a pure virtual method in an abstract class
What is the effect of providing an implementation for a method declared as pure virtual. Doesn't it make the base class non-abstract? Should the Derived class provide an implementation?
class Base
{
public:
Base();
~Base();
virtual void abstractMethod() = 0;
private:
uint32_t data;
};
class Derived : public Base
{
public:
Derived();
~Derived();
};
void Base::abstractMethod() { data = 1; }
+3
source to share
1 answer
A pure virtual function implementation does not change the rules. The function is still pure, the class is still abstract, and the derived class must still override it with an impure function that must be implemented.
If a pure function has an implementation, it can be called non-virtual:
object.Base::abstractMethod();
This can be useful if there is a generic implementation (or partial implementation) that some derived classes can use; they can simply implement their override to invoke that implementation.
+4
source to share