void flyMatch(Flyable* f1, Flyable * f2)
为什么形参Flyable* f1可以调用plane的指针, 是应为plane是Flyable的子类么??
为什么形参Flyable* f1可以调用plane的指针, 是应为plane是Flyable的子类么??
2015-10-23
/**
这就是所谓的多态啊
*/
class Father{
public:
virtual void func(){std::cout<<"father"<<std::endl;}
};
class Child:public Father{
public:
// override Father's function func()
void func(){std::cout<<"child"<<std::endl;}
};
// usage
Father *f = new Father();
f->func(); // output "father"
Child c = new Child();
c->func(); // output "child"
Father *pf = NULL;
Father *pc = NULL;
pf = c; // pointer of Father type points to object of Child which is Father's subclass
pf->func(); // output "child", because the real type of the object is Child. (polymorphic)
//pc = f; // this is WRONG!! child pointer can not points to father object
pc = dynamic_cast<Child*>(pf); // right举报