473,395 Members | 1,456 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Virtual destructors

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
3 2164
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: qazmlp | last post by:
When a member function is declared as virtual in the base class, the derived class versions of it are always treated as virtual. I am just wondering, why the same concept was not used for the...
11
by: Bonj | last post by:
Hello, Can anyone help me with these fairly simple questions. 1) What is the point in virtual destructors - I've heard it's a good thing for base-classes, but what are the advantages and...
11
by: santosh | last post by:
Hello, I was going through the Marshal Cline's C++ FAQ-Lite. I have a doubt regarding section 33.10. Here he is declaring a pure virtual destructor in the base class. And again defining...
5
by: druberego | last post by:
I read google and tried to find the solution myself. YES I do know that you can get undefined references if you: a) forget to implement the code for a prototype/header file item, or b) you forget...
9
by: desktop | last post by:
On this page: http://www.eptacom.net/pubblicazioni/pub_eng/mdisp.html Shape specify the virtual function: virtual double Intersect( const Shape& s) = 0; then the derived class Circle...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.