Code: Select all
class objectA{
public:
virtual void DoSomethingFor();
};
class objectB : public objectA {
public:
virtual void DoSomethingFor();
};
Its is sort of like you have a instance with layers of functionality brought by polymorphism and additional functionality through inheritance.
class D(class C(class B(class A)))
The problem I have is that it is all contained in a single instance. I would like for this to become dynamic at run-time so that the instance of each inherited class is seperate in contrast to combined.
The only way I can model the wanted behavior, or show it is with:
Code: Select all
class ObjectA
{
private:
uint_t data;
public:
void DoSomethingFor();
void DoNothingFor();
};
class ObjectAI
{
private:
ObjectA *pb;
public:
ObjectAI()
{
pb = new ObjectA();
}
ObjectAI(ObjectA *_pb)
{
pb = _pb;
}
virtual void DoSomethingFor()
{
pb->DoSomethingFor();
}
virtual void DoNothingFor()
{
pb->DoNothingFor();
}
};
class ObjectB
{
public:
void DoSomethingFor();
void DoNothingFor();
}
class ObjectBI : public ObjectAI
{
private:
ObjectB *pd;
ObjectBI()
{
pb = new ObjectB();
}
ObjectBI(ObjectB *_pb)
{
pb = _pb;
}
virtual void DoSomethingFor()
{
pb->DoSomethingFor();
}
virtual void DoNothingFor()
{
pb->DoNothingFor();
}
};
ObjectAI a1(), a2(), a3();
....
ObjectBI b1(&a3), b2(&a1), b1(&a2);
b1.DoSomethingFor();
I am wondering is there a easier way to do this than by making two seperate classes for each actual class: one for the actual implementation, and another for the delegation to the actual implementation instance?
<edit>
Let me append on more alternative question that might open the door for ideas. The question is; Is there any way to automatically generate this delegation class during compile time? So that it is not necessary to write a delegation class for every real class that participates in this model.
</code>