Using the base class pointer can reach the derived class content, in general,I think. Maybe I think it is like a train, as long as I find the head of it,I can reach all of them which is public.
However, when I try to do something like this, I got an error that the base class cannot find the derived class's member function. Although I use a base class pointer variable to record the derived class address, I think it can also reach derived class member function with the head of train. While the fact proved it cannot. So I wonder what the design idea of it. Why it is designed not like Python can do? What is the advantage of this design idea?
The error is prompted as blow:
Sketch map:
Demo code:
#include <iostream>
using namespace std;
class B{
public:
void helloB(){
cout << "hello B" << endl;
}
};
class C:public B{
public:
void helloC(){
cout << "hello C" << endl;
}
};
class A{
public:
B* t;
void helloA(){
t->helloC();
}
};
int main(){
A a;
C c;
a.t = &c;
a.helloA();
}