Suppose I have a pair of base classes:
class A
{};
class B
{
virtual void foo(A* ref);
virtual void foo2(A* ref);
};
And from them, a few derived classes:
class C : virtual public A
{
int data;
};
class D : public B
{
virtual void foo(A* ref)
{
((C*) (ref)).data = 5;
}
};
class E : virtual public A
{
int otherData;
};
class F : public B
{
virtual void foo2(A* ref)
{
((E*) (ref)).otherData = 6;
}
};
And finally, we have a class that follows suit, as such:
class G : public E, public C
{
};
with a main function that goes as follows:
int main()
{
B* myD = new D();
B* myF = new F();
A* myA = new G();
myD->foo(myA);
myF->foo2(myA);
return 0;
}
Ok, IGNORING the obvious fact that this is a 'dreaded diamond' (which, the "multiple As" problem has been averted due to virtual
), the general idea is that I want to have all my objects be Gs, Ds and Fs, but stored as references to As and Bs. I want to be able to use B's (which is actually always either D or F) virtual functions to modify the values of G... However, A and B do not know of any of their child classes, and thus cannot be declared using them. This means that the virtual function arguments must always be a pointer to an A. However, D wants to have it's input always be a C, and F wants it's input to be an E. While this might not have been an issue if G was inheriting from only one parent, our current G is inheriting from 2 parents; I do not know how the this
pointers actually work in the underlying context of situations like this... however, I do not feel like this is going to be a possible situation.
Would anyone be willing to shed some light on the mechanisms of casting pointers to parent/child classes, and if there would be any way this could be pulled off?