It depends on what you mean by require. Usually "require" would mean "fails to compile unless it is done". If that is what you want, then pure virtual is the only want to define the method to achieve that. If you mean, "program won't work unless it is done", then you can provide a default implementation for a virtual method that does gives notice to the user that the implementation is missing.
class BaseClass {
protected:
virtual int printStuff () {
std::cout << "Nothing to print, please override printStuff()."
<< std::endl;
return -1;
}
};
When an implementation for a virtual method is provided (that is, the virtual method is not declared with an assignment of 0
), it is no longer "pure". So this technique satisfies your "not a pure virtual method" requirement, but it is still a virtual method.
I am assuming you want this because you believe some part of your code needs to instantiate a plain BaseClass
. If that is the case, you may consider partitioning BaseClass
into two parts. One that is the "pure" interface, the other that has the stuff you are actually going to use when you instantiate it.
class BaseObject {
//...
};
class BaseClass {
protected:
BaseObject base;
virtual int printStuff () = 0;
};
So you would instantiate a BaseObject
rather than a BaseClass
.