Destruction order

C

Christopher

The FAQ says, "If you are relying on order dependencies in a virtual
inheritance hierarchy, you'll need a lot more information than is in
this FAQ."

Can someone post that information here?

I was under the impression that when a base class has a virtual
destructor, that the destruction order is from most derived to base.

I've got a tricky smart pointer member that is going to depend on this
and wanted to verify that it is always guarenteed.

P.S.
On an aside, the FAQ is FAQ LITE, is there a FAQ Full Flavor?
 
N

Noah Roberts

The FAQ says, "If you are relying on order dependencies in a virtual
inheritance hierarchy, you'll need a lot more information than is in
this FAQ."

Can someone post that information here?

I was under the impression that when a base class has a virtual
destructor, that the destruction order is from most derived to base.

That is not what the text quoted is referring to. They are referring
specifically to virtual *inheritance*.

http://www.cprogramming.com/tutorial/virtual_inheritance.html
 
K

Krice

I've got a tricky smart pointer member that is going to depend on
this and wanted to verify that it is always guarenteed.

If you want to do funny unneccessary things in C++ you can
always test the order of destruction. Add a routine in the
destructor that prints something on screen.
 
C

Christopher

If you want to do funny unneccessary things in C++ you can
always test the order of destruction. Add a routine in the
destructor that prints something on screen.

I don't see why it would be funny or unecessary...
In fact, I don't see a way around this particular situation at all,
but then I am not as familiar with the part of boost, that I am
delving into at this point in time, as I should be.

I've already debugged through it. I am looking for the _guarentee_ and
understanding. Just because something debugs one way one day, doesn't
mean it will be the same after people get a hold of it, derive from
it, and it is in production. To trust your debugger for a hard fast
rule would be foolish.
 
A

Alain Ketterlin

Christopher said:
The FAQ says, "If you are relying on order dependencies in a virtual
inheritance hierarchy, you'll need a lot more information than is in
this FAQ."

Can someone post that information here?

I was under the impression that when a base class has a virtual
destructor, that the destruction order is from most derived to base.

Virtual destructors/members and virtual inheritance are two different
things. The remark above applies to virtual inheritance (i.e., when a
class has a public virtual base class).

Your question is much simpler. Yes, destructors are applied from derived
to base. If it were the reverse, you would be able to run code in a
derived dtor on an "incomplete" object (an object that has already
undergone partial destruction).
I've got a tricky smart pointer member that is going to depend on this
and wanted to verify that it is always guarenteed.
Yes.

P.S.
On an aside, the FAQ is FAQ LITE, is there a FAQ Full Flavor?

If the FAQ you mention is the one at
http://www.parashift.com/c++-faq-lite/, then the answer to your question
is... a faq (section [3]).

-- Alain.
 
K

Krice

I don't see why it would be funny or unecessary...

It's funny when you manage to confuse a fundamental feature of
a programming language.
In fact, I don't see a way around this particular situation at all

Don't use smart pointers. It looks like they aren't that smart
after all.
 
W

Werner

If you want to do funny unneccessary things in C++ you can
always test the order of destruction. Add a routine in the
destructor that prints something on screen.

Good advice.
 
W

Werner

Don't use smart pointers. It looks like they aren't that smart
after all.

Why give him this advice? You're not making sense. Give
reasons as to why not to use smart pointers vs. why to use
them if you want to be of help. True, he confusing
fundamentals.

One good reason for using a smart pointer is when
you forget to initialize it, it has a consistent
state. Yes, finding uninitialized pointers in a million
lines of source can be daunting. Finding which one causes
the bug, well, just rather replace them all.

Regards,

Werner
 
R

Richard Damon

The FAQ says, "If you are relying on order dependencies in a virtual
inheritance hierarchy, you'll need a lot more information than is in
this FAQ."

Can someone post that information here?

I was under the impression that when a base class has a virtual
destructor, that the destruction order is from most derived to base.

I've got a tricky smart pointer member that is going to depend on this
and wanted to verify that it is always guarenteed.

P.S.
On an aside, the FAQ is FAQ LITE, is there a FAQ Full Flavor?

The standard is very clear on the order that a class hierarchy is
constructed, (and by the reverse order rule, destructed). The basic rule
is that when a constructor for a class is entered, it first calls the
constructor from base classes, in a left to right order, then the
constructors for its member variables in deceleration order.

The one hiccup to this order is virtual base classes, as those may be
mentioned several places in the inheritance tree, but all these
represent a single sub-object that will only be constructed once. I
don't know if the standard gives a precise timing of when this object is
created, but I am fairly sure that it is guaranteed to have been created
by at least the point where it have been by the basic rule. I suspect
that virtual base classes will need to be created before any of the
"normal" base classes, but I don't know it that is promised by the
standard. Note that if you inherit from a base class that has a virtual
base class, even if you do not specify that you inherit from that
virtual base, your constructor is still going to need to construct that
virtual base, it doesn't get deferred to your base that declared the
relationship.

As has been pointed out, this (and what the FAQ is referring to) is
virtual inheritance, "class foo: virtual pubic bar, public baz {}" not
just a virtual destructor. A virtual destructor just make sure that the
right destructor gets called if you delete an object through a pointer
to a base class (without the virtual destructor, this operation causes
undefined behavior).
 
C

Christopher

It's funny when you manage to confuse a fundamental feature of
a programming language.


Don't use smart pointers. It looks like they aren't that smart
after all.

Not understanding a term "virtual inheritance", due to never using it
or seeing it before, while being experienced and understanding
deriving classes and virtual destructors is what I assume you are
referring to.

To assert that fact as a reason as to why my design, which you haven't
seen at all, does something "funny or uneccessary" is a
philosophically unsound argument.

Unless of course, my design used or should use "virtual inheritance".
Neither is the case.
 
W

Werner

Not understanding a term "virtual inheritance", due to never using it
or seeing it before, while being experienced and understanding
deriving classes and virtual destructors is what I assume you are
referring to.

Well, perhaps I should not give you this link, as you might have
found it yourself already. I understand that to look for something,
you need to know about it. In case you hadn't:

http://en.wikipedia.org/wiki/Virtual_inheritance

As far as construction/destruction order goes, I can't explain it
better than the mentioned FAQ's.

Just search a little bit more ;-) [hint: multiple inheritance]. I'm
mentioning this as it seems you might have not heard this terminology
before, and when you look for something and you don't know what you
are looking for, it's hard...

Furthermore, I might mention that the order of destruction is always
the exact opposite to the order of construction. The order of
destruction is defined in the c++ standard par 12.6.2:10.

Kind regards,

Werner
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top