473,498 Members | 1,528 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Virtual destructor -- what is correct?

Consider the following:

class A
{
public:
A() { }
~A() { }
};

class B : public A
{
public:
B() { }
virtual ~B() { }
};

class C : public B
{
public:
C() { }
};

int main()
{
B *pB = new C;
delete pB;
}

I am having a debate of whether the "delete pB" invokes undefined behavior.
I believe it does invoke undefined behavior.

However someone else has claimed that since the base pointer defined in
main() is to a B, and B has a virtual destructor, deleting
through a pointer to B, is safe (no undefined behavior).

My claim is that regardless of whether B has a virtual destructor, the base
class A, has a non-virtual destructor,
therefore the code invokes undefined behavior.

Who is correct in this regard?

Paul

Jul 22 '05 #1
2 2653

"Paul McKenzie" <pa**@paul.com> wrote in message news:92******************************@news.1usenet .com...
However someone else has claimed that since the base pointer defined in
main() is to a B, and B has a virtual destructor, deleting
through a pointer to B, is safe (no undefined behavior).


This is the correct answer. It is the static type passed to delete that
must have the virutal destructor.
Jul 22 '05 #2
Paul McKenzie wrote:
Consider the following:

class A
{
public:
A() { }
~A() { }
};

class B : public A
{
public:
B() { }
virtual ~B() { }
};

class C : public B
{
public:
C() { }
};

int main()
{
B *pB = new C;
delete pB;
}

I am having a debate of whether the "delete pB" invokes undefined
behavior. I believe it does invoke undefined behavior.

However someone else has claimed that since the base pointer defined
in main() is to a B, and B has a virtual destructor, deleting
through a pointer to B, is safe (no undefined behavior).

My claim is that regardless of whether B has a virtual destructor, the
base class A, has a non-virtual destructor, therefore the code invokes
undefined behavior.

Who is correct in this regard?


I'm afraid "someone else" is correct. If you want to delete a C through
a pointer to B, the system must dispatch the destructor call to the
right class (i.e. C instead of B), and for that, a virtual destructor
in B is sufficient.

Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1946
by: Jimmy Johns | last post by:
Hi all, I have some class hierarchy as follows: class S {}; class A {}; class B {public: vector<S*> v_; virtual ~B();}; class C : public virtual A, public virtual B { // do I need to define...
39
736
by: Ele | last post by:
Is it correct to say that Whenever a class has a virtual member function, define its destructor as "virtual"? Can a destructor as "pure virtual"? When is it needed to do so? For an interface,...
23
2343
by: ctick | last post by:
A reason for declaring a "virtual destructor" for a Base class is to make sure the destructor of Derived class will be invoked when a pointer of Base type is used to delete an object of Derived. ...
11
4321
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...
26
541
by: Prawit Chaivong | last post by:
Hi All, There is code here. ------------------------------------------------------------------ class Base{ public: Base(){} virtual ~Base(){} private: int a;
27
2097
by: tuvok | last post by:
Is it correct that the virtual dtor of base gets called implicitly? Here's some code to demonstrate what I mean: Class B has a virtual destructor, so has class D which is derived from B. Deleting...
7
2009
by: dc | last post by:
Can anybody think of a situation where virtual function's address resolution/ something related to virtual is done at compile time
17
3510
by: Jess | last post by:
Hello, If I have a class that has virtual but non-pure declarations, like class A{ virtual void f(); }; Then is A still an abstract class? Do I have to have "virtual void f() = 0;"...
7
7318
by: Tonni Tielens | last post by:
I'm trying to create a pure virtual class describing an interface. Normally, when I do this I make the destructor pure virtual so that, even if there are no members in the class, it cannot be...
0
7124
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
7200
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6884
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...
0
7375
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5460
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4904
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3090
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
287
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.