473,480 Members | 2,277 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Multiple inheritance with only one virtual base destructor

Does the following code produce undefined behaviour?

class Base
{
public:
virtual ~Base();
/* ... */
};

class BaseWithNonVirtualDestructor
{
protected:
~BaseWithNonVirtualDestructor();
/* ... */
}

class Derived : public Base, public BaseWithNonVirtualDestructor
{
public:
/* ... */
};

int main()
{
Base *ptr = new Derived;
delete ptr;
}
May you also point me to the section of the standard which defines the
behaviour of such MI constructs in conjunction with virtual destructors?
For my current problem, by the way, BaseWithNonVirtualDestructor is
actually boost::noncopyable, which I would like to use in this manner.
--
Christian Hackl
Apr 27 '07 #1
4 3924
Christian Hackl wrote:
Does the following code produce undefined behaviour?
No.
>
class Base
{
public:
virtual ~Base();
/* ... */
};

class BaseWithNonVirtualDestructor
{
protected:
~BaseWithNonVirtualDestructor();
/* ... */
}

class Derived : public Base, public BaseWithNonVirtualDestructor
{
public:
/* ... */
};

int main()
{
Base *ptr = new Derived;
delete ptr;
}
May you also point me to the section of the standard which defines the
behaviour of such MI constructs in conjunction with virtual
destructors?
I think you're looking for 5.3.5/3 and 12.4/7.
For my current problem, by the way, BaseWithNonVirtualDestructor is
actually boost::noncopyable, which I would like to use in this manner.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 27 '07 #2
Victor Bazarov ha scritto:
Christian Hackl wrote:
>Does the following code produce undefined behaviour?

No.
Thank you!
>May you also point me to the section of the standard which defines the
behaviour of such MI constructs in conjunction with virtual
destructors?

I think you're looking for 5.3.5/3 and 12.4/7.
Thanks again for dispelling my doubts :)
--
Christian Hackl
Apr 27 '07 #3
Christian Hackl wrote:
Does the following code produce undefined behaviour?
In addiction to what Victor said, it also makes sense that your code
works properly: the virtual destructor is required to be able to
discover the dynamic type. So, as long as you deallocate from a base
class that has got the virtual destructor, you will be sure that the
destructor of the appropriate dynamic type will be allocated. That
destructor will call all the destructors of the base classes.

Regards,

Zeppe
Apr 27 '07 #4
On Apr 27, 9:15 pm, Christian Hackl <h...@sbox.tugraz.atwrote:
Does the following code produce undefined behaviour?
class Base
{
public:
virtual ~Base();
/* ... */
};
class BaseWithNonVirtualDestructor
{
protected:
~BaseWithNonVirtualDestructor();
/* ... */
}
class Derived : public Base, public BaseWithNonVirtualDestructor
{
public:
/* ... */
};
int main()
{
Base *ptr = new Derived;
delete ptr;
}
May you also point me to the section of the standard which defines the
behaviour of such MI constructs in conjunction with virtual destructors?
Victor has answered the exact question. In fact, multiple
inheritance has nothing to do with it; if the static type of the
pointed to object in the delete has a virtual destructor, you're
OK; if it doesn't you're not.
For my current problem, by the way, BaseWithNonVirtualDestructor is
actually boost::noncopyable, which I would like to use in this manner.
Presumably, it makes no sense for people to manipulate your
object using boost::noncopyable*. So why not inherit privately
from the class, so they can't (except by means of a C-style
cast)? As your class now stands:

BaseWithNonVirtualDestructor* p = new Derived ;
delete p ;

*is* undefined behavior, and doesn't solitict the slightest
complaint on the part of the compiler.

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Apr 28 '07 #5

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

Similar topics

0
4237
by: Lefevre | last post by:
Hello I recently had troubles with a class inheritance hierarchy. I solved it, but it didn't satisfied me. I found the solution using this forum :) Actualy i found the following message...
5
2647
by: Mark | last post by:
When declaring a class that uses multiple inheritance, does the order used when listing the inheritance matter? I'm finding with my compiler (gcc 3.2.2) that my program seg faults when destructing...
2
4536
by: Chunhui Han | last post by:
Hi, I was recently reading about virtual base classes in C++. The book I was reading says that it is illegal to have non-virtual destructor for the virtual base class. It seems to me that...
9
372
by: nicolas.hilaire | last post by:
Hi all, I have somes classes that have the same concept. I would like to instance one of them depending on a condition Something like this MyObj myObj; if (i==0)
5
2081
by: ronak2121 | last post by:
All, I am getting a linking error when trying to use multiple inheritance with virtual methods. Here is some sample code which illustrates my problem. class A { };
60
4862
by: Shawnk | last post by:
Some Sr. colleges and I have had an on going discussion relative to when and if C# will ever support 'true' multiple inheritance. Relevant to this, I wanted to query the C# community (the...
3
2625
by: ernesto | last post by:
Hi everybody I have the following class declarations: class Interface { public: virtual char* getName() const = 0; }; class BaseClass : public Interface {
1
1142
by: Michael Hines | last post by:
Hello, I have a class factory that supports single inheritance but it is an error if the base appears twice. e.g class Foo(hclass(h.Vector), hclass(h.List)): should raise an exception since...
4
3917
by: EnsGabe | last post by:
Suppose you have a class heirarchy as such: class Base{ .... }; class Mid1 : public Base{ ....
0
6911
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
7050
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,...
1
6743
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
6966
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
5344
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
4787
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
2999
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
2988
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1303
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.