How to call Aggregator class destructor?

H

haries

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
 
K

Kai-Uwe Bux

haries said:
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
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top