In C++, I frequently run into this problem and always left confused. Suppose there are multiple levels of classes. Each level is instantiating the class which is a level below. E.g. below level1 instantiates level2_a and b(there are more in real case). Now some operation the leaf level object needs to perform. Simple example, say the leaf level object needs to dump some information onto a status console. And all the leaf level objects need to do this. What is the best way to share this "status console" pointer amongst the objects (there could be 100s of these objects)?
- Do they all need to store a pointer to it?
- Or pass the "status console" pointer to some member function call which it can then use to dump a log.
Another such example they all need to share a stack onto which they convey some info when they are destroyed? How to share the stack pointer amongst all these leaf level objects
Example:
class level2_a
{
<properties here differ from level2_b>
public:
~level2_a()
{
// dump some info into a common stack here
}
void dump_change_to_value(int newval)
{
// need a console pointer here
// but can't be singleton because there's 1 per window
}
};
class level2_b
{
<properties here differ from level2_a>
public:
~level2_b()
{
// dump some info into a common stack here
}
void dump_change_to_value(int newval)
{
// need a console pointer here
// but can't be singleton because there's 1 per window
}
};
class level1
{
private:
level2_a* ml2_a;
level2_b* ml2_b;
public:
<func members..>
};
How to accomplish sharing of the stack/console amongst level2_a and b