By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,194 Members | 859 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,194 IT Pros & Developers. It's quick & easy.

Virtual destructors

P: n/a
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)?
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Dave wrote:
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() ....
Jul 22 '05 #2

P: n/a
Dave wrote:
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.

Jul 22 '05 #3

P: n/a
Dave wrote:

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.

Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.