請教一下
class Base{
public:
virtual void func{cout<<"Base"<<endl;}
};
class Derived1:public Base{
public:
void func(){cout<<"Derived1"<<endl;}
};
class Derived2:public Derived1{
public:
void func(){cout<<"Derived2"<<endl;}
};
int main(){
Derived2 *ptr_b=new Derived2;
ptr_b->func();
Derived2 *ptr_a=dynamic_case<Derived1 *>(ptr_b);
ptr_a->func();
Base *ptr_bs=dynamic_case<Base *>(ptr_b);
ptr_bs->func();
delete ptr_b;
}
請問為什麼會列印?
ans:
Derived2
Derived2
Derived2
謝謝