I need to access the derived class member variable through Base class variable.
Class A{
};
Class B:public A {
int data;
};
now I need to do something like this
A *pb = new B()
pb->data = 10;
but the problem is I cannot access the Derived member class wihtout it.
and Yeah I know how to make it work with virtual functions.
Thanks, I really appreciate your help.
Short answer: You cannot. Because your compiler does not know what
pbis. it could be of typeA. However, you an usedynamic_cast, which returns aBpointer orNULLif that is not possible.Anyhow, if you need to do that it probably means that you should revise your design as upcasting is not a good idea. Sometimes though, you just cannot avoid it. E.g. when using external libraries and such.