I have classes A1, A2, B, and C. A2 inherits from A1. C inherits from A2 and B, and its constructor initializes all B, A2, and A1 variables.
As a tree, it looks like this:
C
/ \
B A2
|
A1
I have a function Foo that takes a parameter pointer P of type B*, but the only parameters passed will be of type C* (and thus their objects will have all B, A2, and A1 variables initialized)
Inside Foo, I can type cast my P into A2* or A1* pointers, and I'm able to use those functions. The A2 and A1 functions run without complaining, but none of the A2 or A1 variables get recognized (when I debug they're all uninitialized), which leads to bugs.
Can somebody explain why this happens and what I can do to fix it (I can't change Foo's parameter type), or direct me to a source where I can read more about it?
EDIT: when I say I'm explicitly casting my P into A2*, I mean I just do (A2*)P->bar(), so I may be using the wrong type of casting.