4 ms·
You cannot just declare the destructor of a base class, since it will get called by the derived class destructors and linking will then fail. But you can write
by pbsd 11y ago
You cannot just declare the destructor of a base class, since it will get called by the derived class destructors and linking will then fail. But you can write out an implementation for a pure virtual function, which will still keep the base class uninstantiable:
virtual ~AbstractBaseClass() = 0;
...
AbstractBaseClass::~AbstractBaseClass() { }
- anon4 11y agoHuh. Does that mean I can write class A { public: virtual void foo() = 0; }; void A::foo() {} class B: public class A {}; B b; b.foo(); and that will work?
- pbsd 11y agoNo, it will complain that A::foo is pure virtual, and that B must implement `foo`. However, if you define in B the method `void foo() { A::foo(); }`, it will work. In destructors this happens implicitly. There was an old Herb Sutter GotW about this: http://www.gotw.ca/gotw/031.htm http://www.gotw.ca/gotw/031.htm