Virtual destructors

D

Dave

class base
{
public:
virtual ~base();
virtual void foo();
};

class derived: public base
{
public:
~derived();
void foo();
};

derived::foo() does not need to be explicitly designated virtual (though it
is not an error to do so) - it is virtual by virtue of the fact that there
is a member function of the same signature in class base that is virtual.
Along the same lines, is derived::~derived() virtual because base::~base()
is virtual? Or must we explicitly designate derived::~derived() virtual if
we wish it to be so (say, if because we wish to derive from class derived)?
 
G

Gianni Mariani

Dave said:
class base
{
public:
virtual ~base();
virtual void foo();
};

class derived: public base
{
public:
~derived();
void foo();
};

derived::foo() does not need to be explicitly designated virtual (though it
is not an error to do so) - it is virtual by virtue of the fact that there
is a member function of the same signature in class base that is virtual.
Along the same lines, is derived::~derived() virtual because base::~base()
is virtual? Or must we explicitly designate derived::~derived() virtual if
we wish it to be so (say, if because we wish to derive from class derived)?


in the case above, both derived member functions are virtual.

hence :

base * b = new derived();

delete b; // this calls ~derived() ....
 
T

Tim Threlfall

Dave said:
class base
{
public:
virtual ~base();
virtual void foo();
};

class derived: public base
{
public:
~derived();
void foo();
};

derived::foo() does not need to be explicitly designated virtual (though it
is not an error to do so) - it is virtual by virtue of the fact that there
is a member function of the same signature in class base that is virtual.
Along the same lines, is derived::~derived() virtual because base::~base()
is virtual? Or must we explicitly designate derived::~derived() virtual if
we wish it to be so (say, if because we wish to derive from class derived)?

12.4.7 of the standard states:

A destructor can be declared virtual (10.3) or pure virtual (10.4); if
any objects of that class or any derived class are created in the
program, the destructor shall be defined. If a class has a base class
with a virtual destructor, its destructor (whether user- or implicitly-
declared) is virtual.
 
L

lilburne

Dave said:
derived::foo() does not need to be explicitly designated virtual (though it
is not an error to do so) - it is virtual by virtue of the fact that there
is a member function of the same signature in class base that is virtual.
Along the same lines, is derived::~derived() virtual because base::~base()
is virtual? Or must we explicitly designate derived::~derived() virtual if
we wish it to be so (say, if because we wish to derive from class derived)?

You can't revoke virtual, once a function is declared
virtual all subsequent functions with the same signature are
virtual too.

Whilst you don't have to explicitly declare either
derived::foo() or derived::~derived() virtual, it is polite
to do so. That way readers of derived class don't have to
read back through the class heirarchy to discover which
methods are or are not virtual.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top