It is "possible" in some sense (if your goal was that the derived class stays abstract otherwise). But it won't give the result you would like: Because the compiler will create a destructor itself implicitly if the programmer hasn't done so.
It's therefor not possible to force the derived class' author to explicitly declare a constructor.
(edit: Like @chubsdad notes noted, the error in your particular code is because you need to define the explicitly declared destructor of the base class).
Edit: Just for fun, there are situations that necessiate an explicitly declared constructor. Consider the following
struct Viral {
struct Dose { };
protected:
~Viral() throw (Dose) { }
};
struct Base {
virtual ~Base() throw() { }
};
struct Derived : Base, Viral { };
This code won't compile because the implicitly declared ~Derived
will have an exception specification throw (Dose)
which is looser than what ~Base
has - so it violates the requirement that overriders shall not have a looser exception specification. You will need to explicitly declare the destructor appropriately
struct Derived : Base, Viral { ~Derived() throw() { } };
But this is not really a solution to your problem, because derived classes need to "cooperate" into either deriving from Viral
or putting it as a non-static data member. It's also very ugly :)
Edit: The following seems to be a Standard conforming way to do it
struct Viral {
struct Dose { };
protected:
~Viral() throw (Dose) { }
};
struct Base : virtual Viral {
virtual ~Base() throw() { }
};
Clang and GCC (starting with v4.6) reject any derived class of Base
that has an implicitly declared destructor, because it has an incompatible exception specification (any derived class shall call ~Viral
directly, instead of indirectly by calling ~Base
, the Standard says). Comeau accepts this, but I strongly suspect that it is non-conforming in this regard.
void*
can't be done. If you define destructors that won't change anything. The compiler doesn't magically know the type of what you delete from avoid*
just becaus it has a destructor defined. Have you tried this and it works? – Machmeterreturn 0;
in the code … – Homagerauto_ptr<void*>
and expect it to destroy the underlying type. You could use aauto_ptr<VBase>
though, THAT would make sense. – Accommodatereturn 0;
can be omitted frommain
. Don’t ask me why but that’s the way it is. – Homagerreturn 0
falls into the latter category IMHO. – Administrationmain
must beint
and (only in C++) it must not be omitted but (only in C++) you don’t need the explicitreturn 0;
. – Homager