Re: Pure virtual destructor
On 2007-12-25 23:07:50 -0600, johanatan <johanatan@gmail.com> said:
Actually, looks like he already has the implementation defined. He
simply needs to remove '= 0' from the declaration. So, there's really
two problems: the first is trying to make the destructor pure virtual,
and the second is trying to define the destructor in the base class
after having made it pure virtual!!
A pure virtual destructor serves a different need than the typical pure
virtual function. The syntax exists to allow you to define a virtual
destructor, and still have the class treated as abstract. This is
useful in certain cases where you want to define an abstract interface
or tag class through which a derived object can be deleted. Without the
=0 in the destructor, you'd have to define another pure virtual
function in order to make the base class abstract.
For instance,
class Tag
{
public:
virtual ~Tag() = 0;
};
Tag::~Tag() {}
class Foo: public Tag
{
public:
Foo();
virtual ~Foo();
};
Foo::Foo() {}
int main()
{
//Tag t; // <-- error, abstract.
Tag* pt = new Foo(); // <-- ok.
delete pt; // <-- ok, Foo::~Foo is called.
}
-dr