Lets say I have a class A
and I also have classes B and C that inherit from class A
class A {
virtual void foo()=0;
//stuff
}
class B : class A {
void foo();
//more specific stuff
}
class C : class A {
void foo();
//more specific stuff
}
Now lets say I have a similar set up for classes X, Y, and Z, except I want them contain some form of class A
as such.
class X {
A m_myA;
// I know this doesn't work due to the abstract definition, but this is my goal
}
class Y : class X {
B m_myA;
}
class Z : class X {
C m_myA;
}
My question is, what happens to class A in classes Y and Z? I think Y will contain an A and a B, and Z will contain an A and a C.
However, I want a pointer to a generic class X to call foo() of the most derived form of class A it contains.
So if I say
p_x->m_myA.foo();
If p_x is a class Y, m_myA.foo() [class B version] is called
If p_x is a class Z, m_myA.foo() [class C version] is called
Is this possible? If yes, how?
If I don't declare some version of class A in base class X, there is no way to know its a member in all derived classes