473,406 Members | 2,705 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,406 software developers and data experts.

Delete a class with multiple inheritance through base pointer?

Ian

Given

struct A{...};
struct B{...};

struct C : public A, public B {...}

int main()
{
A* a = new C;

delete a;

return 0;
}

Will the B part of a be correctly deleted?

Ian

Jul 22 '05 #1
7 2686

"Ian" <no***@nowhere.com> wrote in message
news:40**************@nowhere.com...

Given

struct A{...};
struct B{...};

struct C : public A, public B {...}

int main()
{
A* a = new C;

delete a;

return 0;
}

Will the B part of a be correctly deleted?

Ian


Its not a question of whether or not the B part will be correctly deleted.
Its a question of whether the code is correct or not, if it is not the
consequences are undefined.

The answer is that the code is correct if A has a virtual destructor,
otherwise you have undefined behaviour.

john
Jul 22 '05 #2
Ian wrote:

Given

struct A{...};
struct B{...};

struct C : public A, public B {...}

int main()
{
A* a = new C;

delete a;

return 0;
}

Will the B part of a be correctly deleted?
only if A has a virtual destructor.

i.e.

struct A{ virtual ~A(){} ...}; // not the virtual destructor
struct B{...};

struct C : public A, public B {...}

int main()
{
A* a = new C;

delete a;

return 0;
}

Ian


Jul 22 '05 #3
Ian
I left out the virtual destructors in A and B...

Should work, but I am seeing problems where it does not.

Ian wrote:

Given

struct A{...};
struct B{...};

struct C : public A, public B {...}

int main()
{
A* a = new C;

delete a;

return 0;
}

Will the B part of a be correctly deleted?

Ian


Jul 22 '05 #4

"Ian" <no***@nowhere.com> wrote in message
news:40**************@nowhere.com...
I left out the virtual destructors in A and B...

Should work, but I am seeing problems where it does not.


What problems are you seeing?

john
Jul 22 '05 #5
Ian
John Harrison wrote:
"Ian" <no***@nowhere.com> wrote in message
news:40**************@nowhere.com...
I left out the virtual destructors in A and B...

Should work, but I am seeing problems where it does not.

What problems are you seeing?

What I think is a compiler bug.... Members of one base being invalid in
the destructor of the derived class.

Ian

Jul 22 '05 #6

"Ian" <no***@nowhere.com> wrote in message
news:40**************@nowhere.com...
John Harrison wrote:
"Ian" <no***@nowhere.com> wrote in message
news:40**************@nowhere.com...
I left out the virtual destructors in A and B...

Should work, but I am seeing problems where it does not.

What problems are you seeing?

What I think is a compiler bug.... Members of one base being invalid in
the destructor of the derived class.

Ian


Compiler bugs are always a possibility but the code you posted is very
simple (maybe your real code is more complex) so I'd be surprised if a
compiler got that wrong.

john
Jul 22 '05 #7

"Ian" <no***@nowhere.com> wrote in message
news:40**************@nowhere.com...
John Harrison wrote:
"Ian" <no***@nowhere.com> wrote in message
news:40**************@nowhere.com...
I left out the virtual destructors in A and B...

Should work, but I am seeing problems where it does not.

What problems are you seeing?

What I think is a compiler bug.... Members of one base being invalid in
the destructor of the derived class.


Could you elaborate on that (probably with some sample code & which compiler
you're using). From your code I do not see a problem (with the virtual
declartions being added) as the order of destruction is simply the reverse
order of construction. Thus the dtor of the derived class is called before
the one of the base class.

Regards
Chris
Jul 22 '05 #8

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

Similar topics

2
by: Ian McBride | last post by:
(was: delete() confusion) I have a class with multiple base classes. One of these base classes (base1) has its own new/delete operators and nothing else. Another base class (base 2) has a...
8
by: Bryan Parkoff | last post by:
I find an interesting issue that one base class has only one copy for each derived class. It looks like that one base class will be copied into three base classes while derived class from base...
4
by: Joel | last post by:
I have this bug that quite puzzled me. Basically I am having a segmentation fault on deleting an object, which belongs to a class which is the result of multiple inheritance from two other classes....
0
by: cppsks | last post by:
Hello. I posted a question regarding this yesterday. I came up with the following solution but I am a little hesistant as to this solution having any side-effects that I am not aware of. The...
0
by: cppsks | last post by:
Hello. I posted a question regarding this yesterday. I came up with the following solution but I am a little hesistant as to this solution having any side-effects that I am not aware of. The...
14
by: cweisbrod | last post by:
All, I'm seeing something very strange and was hoping someone might have some insight into the cause of what I'm seeing. I have the following scenario: Platform: Microsoft Windows XP Pro,...
3
by: hurcan solter | last post by:
Consider the code fragment; class A { public: A(){} A(int prm):mprm(prm){} int mprm; }; class B:public A {
11
by: Rahul | last post by:
Hi Everyone, While working with Java, i came across super() which passes values to base class constructor from derived class constructor. I was wondering if this could be implemented in c++ by...
3
by: Rahul | last post by:
While reading Inside the C++ object model I came through the following paragraph Point3d origin, *ptr = &origin; A) origin.x = 0.0; B) ptr->x = 0.0; The book says "A & B statements performs...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
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
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,...
0
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...

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.