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