【发布时间】:2013-08-19 03:44:46
【问题描述】:
此代码无法使用 gcc 4.7.0 编译:
class Base
{
public:
Base(const Base&) = delete;
};
class Derived : Base
{
public:
Derived(int i) : m_i(i) {}
int m_i;
};
错误是:
c.cpp: In constructor `Derived::Derived(int)´:
c.cpp:10:24: error: no matching function for call to `Base::Base()´
c.cpp:10:24: note: candidate is:
c.cpp:4:2: note: Base::Base(const Base&) <deleted>
c.cpp:4:2: note: candidate expects 1 argument, 0 provided
换句话说,编译器不会为基类生成默认构造函数,而是尝试调用 deleted 复制构造函数作为唯一可用的重载。
这是正常行为吗?
【问题讨论】:
-
是的,因为如果有任何构造函数是用户声明的,那么默认构造函数就会被抑制。
-
为什么删除构造函数被认为是构造函数的声明?这是反直觉的。
标签: c++ c++11 deleted-functions