我的类base
只包含私有默认构造函数和公共删除的复制构造函数,没有别的。
class base {
private:
base() = default;
public:
base(const base&) = delete;
};
如果我尝试继承base
并创建derived
如下类的实例,g++ 4.8.2 不会编译我的代码,但 VC++ 2013 会。
class derived : public base {
private:
derived() = default;
};
derived x;
那么,它是 g++ 或 VC++ 2013 中的一个错误,只是忽略了一些东西吗?
这是完整的代码...
class base {
private:
base() = default;
public:
base(const base&) = delete;
};
class derived : public base {
private:
derived() = default;
};
derived x;
int main() {
}
...和 g++ 错误消息。
main.cpp:12:5: error: 'constexpr derived::derived()' is private
derived() = default;
^
main.cpp:15:9: error: within this context
derived x;
^
main.cpp: In constructor 'constexpr derived::derived()':
main.cpp:3:5: error: 'constexpr base::base()' is private
base() = default;
^
main.cpp:12:5: error: within this context
derived() = default;
^
main.cpp: At global scope:
main.cpp:15:9: note: synthesized method 'constexpr derived::derived()' first required here
derived x;
^