When I write interface classes in C++, I choose either of the following 2 options
class Interface
{
public:
virtual R1 f1(p11, p12 , ...) = 0;
...
virtual Rn fn(pn1, pn2 , ...) = 0;
virtual ~Interface() {}
}
or
class Interface
{
public:
virtual R1 f1(p11, p12 , ...) = 0;
...
virtual Rn fn(pn1, pn2 , ...) = 0;
virtual ~Interface() = 0;
}
Interface::~Interface() {}
The first version is shorter to write
The second is attractive in that all functions of the interface are pure virtual
Is there any reason I should prefer one or the other method (or perhaps a third one)?
Thanks
As I understand, the purpose of making virtual function pure virtual is to force the derived classes to either provide implementation for it or choose the default implementation by explicitly writing Base::f()
in the Derived::f()
.
So if that is true, then what is the purpose of making virtual destructor pure virtual
? Does it force the derived classes to provide implementation for Base::~Base()
? Can the derived classes implement Base::~Base()
? No.
So that means, the first version with virtual
destructor seems enough for almost all purposes. After all, the most common purpose of virtual destructor is that the clients can correctly delete
objects of the derived classes through pointers of type Base*
.
However, if you make all functions in Base
virtual only, not pure virtual, and provide implementations for them (actually you've to provide), and at the same time you want to make Base
abstract type, then having a pure virtual destructor in Base
is the only solution:
class Base
{
public:
virtual void f() {}; //not pure virtual
virtual ~Base() = 0; //pure - makes Base abstract type!
};
Base::~Base() {} //yes, you have to do this as well.
Base *pBase = new Base(); // error - cannot create instance!
Hope that helps.
To me, the dtor is not part of the interface. The fi() would have analogues in other languages, not the dtor. Likewise, you could write pre- and post- conditions for the fi(), but not the dtor. This makes it just a C++ wart, and the first technique is the most comfortable way of dealing with it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With