构造父类的有参构造函数?怎么最好?

来源:百度知道 编辑:UC知道 时间:2024/05/09 20:04:06
这是我想出来的一个办法!还有没有别的???
class a
{
public:
int k;
a(){};
a(int t){
k=t;
cout<<"a"<<endl;};
};
class b:public a
{
public:
b(int e){a::a(e);cout<<"b"<<endl; };
};

你这样不就是一个调用么?
事实上,父类的构造肯定在子类之前完成!如果你需要调用子类的参数,直接利用参数列表就行了,这个因该是最合理的
#include<iostream>
using namespace std;
class a
{
public:
int k;
a(){}
a(int t)
{
k=t;
cout<<"a"<<endl;
cout<<k<<endl;
}
};
class b:public a
{
public:
b(int e):a(e){cout<<"b"<<endl;}
};
int main()
{
a x;
a y(2);
b z(3);
return 0;
}
VC合理通过!

你是想别人夸你,还是骂你哦,
有好好的标准不用,不是有个初始化表哦,
干嘛要搞得这样复杂啊,
而且效率肯定了没有标准好.