【发布时间】:2015-11-16 14:19:30
【问题描述】:
我写了一个测试程序:
#include <iostream>
class base
{
public:
base()
{
std::cout << "base 1" << std::endl;
}
base(int a)
{
std::cout << "base a=" << a << std::endl;
}
};
class child : public base
{
public:
child()
{
std::cout << "child" << std::endl;
}
};
int main(int argc, char* argv[])
{
child c;
return 0;
}
程序输出以下内容:
$ ./a.out
base 1
child
是否有任何可能的方法将整数参数发送到基类,从而调用另一种形式的基构造函数?
如果 base 只有一个构造函数,我会收到编译错误:candidate expects 1 argument, 0 provided,正如您所料。但是如果我给那个构造函数一个默认参数,比如:
base(int a = 10)
然后我的程序编译并运行,输出为:base a=10。 (有趣吗?)
有没有办法传入a的变量值?
【问题讨论】:
标签: c++ inheritance