Iva*_*nov 16 c++ inheritance copy-constructor c++11
我有以下代码:
class C {
public:
C(int) {}
C(const C&) {}
C() {}
};
class D : public C {
public:
using C::C;
};
int main() {
C c;
D d_from_c(c); // does not compile, copy ctor is not inherited
D d_from_int(1); // compiles, C(int) is inherited
}
Run Code Online (Sandbox Code Playgroud)
衍生类应该继承除默认ctor之外的所有基数(这里解释).但为什么复制ctor也不是继承的呢?相关问题的论据在这里是不可接受的.
代码使用g ++ 4.8.1编译.
T.C*_*.C. 18
因为标准是这样说的.[class.inhctor]/p3,强调我的:
对于除了没有参数的构造函数或具有单个参数的复制/移动构造函数之外的候选继承构造函数集中的每个非模板构造函数,构造函数隐式声明具有相同的构造函数特征,除非存在用户声明的构造函数完整类中的相同签名,其中出现using声明或构造函数将是该类的默认,复制或移动构造函数.
| 归档时间: |
|
| 查看次数: |
4736 次 |
| 最近记录: |