By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,305 Members | 1,614 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,305 IT Pros & Developers. It's quick & easy.

How to call Aggregator class destructor?

P: n/a
Hi All,

I have a Aggregator class that aggregates two classes. When
any of the two aggregated
classes die I want the Aggregator to die as well.

How do I call the Aggregator class's destructor from any of the
Aggregated class's destructor?

Thanks in advance.

Hari

Nov 3 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
haries wrote:
Hi All,

I have a Aggregator class that aggregates two classes. When
any of the two aggregated
classes die I want the Aggregator to die as well.
No, you don't want that. The language is designed the other way around: if
an object dies, then so do all its subobjects. That you have a need for
doing it in reverse indicates that your design is seriously off base.

What you really want to do is to redesign your classes so that they go with
the flow of the language and not against it.

Please post some code to illustrate the underlying problem you are trying to
solve. Maybe, you just painted yourself into a corner.

How do I call the Aggregator class's destructor from any of the
Aggregated class's destructor?
Suppose you have an aggregate:

class Agg {
A sub_obj_a;
B sub_obj_b;
...
};

And suppose you also have:

~A::A() {
some black magic calling ~Agg() for the right instance;
}

Then you are in deep waters because ~Agg() calls sub_obj_a.~A() which then
again calls ~Agg(), and so on and so forth. The standard does not even
guarantee this, because it deems calling the destructor of an object twice
undefined behavior already.
Best

Kai-Uwe Bux
Nov 4 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.