Vritual functions casting from base class to derived class (Easy for experienced programmers)
Posted on 2004-12-01
This is the structure of my program:
//some private fields here.
virtual void printObj();
//define printObj() here
class B:public class A //public inheritance
//call the parent virtual function
else if(type B)
//call virtual function from child class
//I have problems here
In my main program I am giving the user option to enter an object of Type A or B into the array. I then just assign object of type b to an array position. B has some extra private members other than parent class. Hence printObj() function is slightly different for class B.
Now I have flags to determine what kind of object an array position holds. Then after checking the flag I just want to call the appropriate printObj() function. If the object is of type A then I have no problems calling the parent class function. But if its of type
B how do it? I tried to use static_cast and tried to cast the object in the array to type B (i.e. parent to child class casting). But I got some weird errors. Whats the appropriate way to do it? Any good experts around here.............
Thanks in advance.