2 one half one_half 于 2014.10.26 14:07 提问

菜鸟 提问 C++ 多态问题 谁能解释一下结果1???

#include
class Base{
public : Base(){}
Base(bool arg){foo();}
virtual void foo(int i=42){cout<<"base "<<i<<endl;}

};
class Derived:public Base
{
public:Derived(){};
Derived(bool arg):Base(arg){foo();}
virtual void foo(int i=12){cout<<"Derived "<<i<<endl;}
};

//结果1:
//Derived 42
//Base 42 ????
int main(){
Derived d;
Base &b1=d;
//Base b2=d;
b1.foo();
//b2.foo();
return 0;
}

//结果2:
//Base 42
//Derived 12
int test(){

Base* b = new Derived(true);
delete b;
return 0;

}

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!