这段代码不能用
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
换句话说,编译器不会为基类生成默认构造函数,而是尝试将已删除的复制构造函数作为唯一可用的重载调用.
这是正常的行为吗?
解决方法
C11§12.1/ 5指出:
A default constructor for a class
X
is a constructor of classX
that can be called without an argument. If there is no user-declared constructor for classX
,a constructor having no parameters is implicitly declared as defaulted (8.4).
你的基地(const Base&)=删除;计为用户声明的构造函数,因此它禁止生成隐式默认构造函数.解决方法当然是声明它:
Base() = default;
原文地址:https://www.jb51.cc/c/113155.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。