h*****g 发帖数: 312 | 1 template
class Foo
{
T tVar;
public:
Foo(T t) : tVar(t) { }
};
class FooDerived : public Foo { };
FooDerived fd;
What is preventing the above code from being legal C++?
A.
The initialization of tVar occurs outside the body of Foo's constructor.
B.
FooDerived uses the non-C++ type std::string.
C.
tVar is a variable of an unknown type.
D.
FooDerived is a non-template class that derives from a template class.
E.
A constructor must be provided in FooDerived.
为啥E 是正确的呢? |
l******l 发帖数: 66 | 2 Because 'FooDerived fd;' needs default constructor. |
h*****g 发帖数: 312 | 3 默认的ctr 不是系统自动给FooDerived fd调用的吗?为啥还得写出来? |
l******l 发帖数: 66 | 4 Because base class already has a ctr, so no system generted ctr.
【在 h*****g 的大作中提到】 : 默认的ctr 不是系统自动给FooDerived fd调用的吗?为啥还得写出来?
|
d*****d 发帖数: 46 | 5 Because the base class's ctor is not a default ctor.
【在 l******l 的大作中提到】 : Because base class already has a ctr, so no system generted ctr.
|
f****4 发帖数: 1359 | 6 class A{
public:
A(int){}
};
class B: public A{
public:
B():A(0){}
}; |
h*****g 发帖数: 312 | 7 哦,因为子类对象创建时 先要调用基类default构造函数,但因为default 被omit了,
所以会出错。
谢谢各位~~~
【在 f****4 的大作中提到】 : class A{ : public: : A(int){} : }; : class B: public A{ : public: : B():A(0){} : };
|
h*****g 发帖数: 312 | 8 那如果在baseclass 自己写一个default ctr 也应该解决问题了吧?
【在 h*****g 的大作中提到】 : 哦,因为子类对象创建时 先要调用基类default构造函数,但因为default 被omit了, : 所以会出错。 : 谢谢各位~~~
|