I did not view it within the C++ Faq lite
How do you define basics class so every class getting it's needed to define a destructor?
I attempted running the program
struct VDtor struct Test:VDtor int primary() remove new Test return
http://codepad.org/wFcE71w3 Using the error
In function `Test::~Test()': t.cpp:(.gnu.linkonce.t._ZN4TestD0Ev+0x1e): undefined mention of the `VDtor::~VDtor()' In function `Test::~Test()': t.cpp:(.gnu.linkonce.t._ZN4TestD1Ev+0x1e): undefined mention of the `VDtor::~VDtor()'
So, is it feasible?
It's "possible" in certain sense (in case your goal could be that the derived class stays abstract otherwise). However it will not provide the result you need: Since the compiler can create a destructor itself unconditionally when the programmer has not done this.
It's because of this not easy to pressure the derived class' author to clearly declare a constructor.
(edit: Like @chubsdad
notes noted, the mistake inside your particular code happens because you have to define the clearly declared destructor from the base class).
Edit: For entertainment, there are situations that necessiate an clearly declared constructor. Think about the following
struct Viral protected: ~Viral() throw (Dose) struct Base struct Derived : Base, Viral
This code will not compile since the unconditionally declared
~Derived may have the best specs
throw (Dose) that is looser than
~Base has - therefore it violates the necessity that overriders shall not have access to a looser exception specs. You will have to clearly declare the destructor properly
struct Derived : Base, Viral
But this isn't really a strategy to your condition, because derived classes have to "cooperate" into either drawing from
Viral or putting it as being a non-static data member. It is also very ugly :)
Edit: The next appears to become a Standard conforming method of doing it
struct Viral protected: ~Viral() throw (Dose) struct Base : virtual Viral
Clang and GCC (beginning with v4.6) reject any derived class of
Base which has an unconditionally declared destructor, because we have an incompatible exception specs (any derived class shall call
~Viral directly, rather than not directly by calling
~Base, the conventional states). Comeau accepts this, however i strongly suspect that it's non-conforming in connection with this.
Every class includes a destructor, regardless. Proclaiming an online destructor within the base guarantees that youngsters may have virtual destructors. This does not imply that the coder will have to clearly declare a destructor -- that would not be a positive thing, anyhow. All this means is the fact that, if your destructor is declared, it will likely be virtual.
struct VDtor VDtor::~VDtor () // <== Implementation. struct Test:VDtor int primary() remove new Test return
To repair the mistake you need to really implement the VDtor::~VDtor() like above.
When Test is destructed, it'll call it's base class destructor, which does not exist. You need to just declare it empty for those who have no necessary destruction logic.