I have the classes A
and B
. B
derives from A
and overloads the method WhoAreYou()
, when I now create a variable of type A
and set the value to a B
object and then call WhoAreYou()
, the method of A
is called. Look at this:
class A{
public:
virtual void WhoAreYou(){
cout << "I am A!"<<endl;
}
};
class B: public A{
public:
void WhoAreYou(){
cout << "I am B!" << endl;
}
};
int main(int argc, char ** argv){
A a = B();
a.WhoAreYou(); //Output: I am A!
}
Is there a way to overload the method so, that in this case the WhoAreYou()
method of B would be called? When I must cast the object first, a method overloading doesn´t really make sense in my opinion...
Thanky for your help!