class Base
{
public:
Base()
{
cout<<"base class"<<endl;
fun();
}
virtual void fun(){cout<<"fun of base"<<endl;}
};
class Derive:public Base
{
public:
Derive()
{
cout<<"derive class"<<endl;
fun();
}
void fun(){ cout<<"fun of derive"<<endl;}
};
void main()
{
Derive d;
}
輸出是:爲什麼我的虛擬方法沒有被覆蓋?
base class
fun of base
derive class
fun of derive
爲什麼第二行是不是fun of derive
?
閱讀材料:[「從不在建築或銷燬過程中調用虛擬函數]](http://www.artima.com/cppsource/nevercall.html)。 – 2010-12-10 02:10:49