在从 Boost 不可复制派生的类派生的类中,是否自动禁止复制?
例如:
class Foo : boost::noncopyable
{
// ...
};
class Bar : public Foo
{
// ...
};
Bar
是不可复制的吗?
For example:
class Foo : boost::noncopyable
{
// ...
};
class Bar : public Foo
{
// ...
};
Is Bar
non-copyable?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
默认情况下,它是不可复制的,除非您创建自定义复制构造函数并避免在那里调用基本复制构造函数。
另请参阅 C++ 中引入的显式默认和删除的特殊成员函数 11.尽管将复制构造函数/运算符设置为私有可以解决问题,但编译器会生成一条远非漂亮和明显的诊断消息,因此 C++11 中删除的复制构造函数/运算符可以解决此问题。
By default it is non-copyable, unless you create a custom copy-constructor and avoid calling a base copy-constructor there.
See also Explicitly-defaulted and deleted special member functions introduced in C++11. Even though making a copy constructor/operator private solves the problem, the compiler generates a diagnostic message that is far from pretty and obvious, so deleted copy constructors/operators are there in C++11 to solve this problem.
假设派生类没有自定义复制构造函数,可以避免调用不可复制的复制构造函数,那么是的。在所有级别,
boost::noncopyable
的所有派生类都是不可复制的。由于派生类的对象还包含boost::noncopyable
的子对象,该子对象是不可复制
的,这意味着如果基类不可复制,则派生类不能被复制,Assuming the derived class doesn't have custom copy-constructor which avoids calling the noncopyable copy-constructor, then yes. At all level, all derived classes of
boost::noncopyable
would be non-copyable. As object of derived class also contains the subobject ofboost::noncopyable
which isnon-copyable
, that means no derived class can be copyable without base-class being copyable,Bar
派生自boost::noncopyable
(即使它不是直接继承),所以是的。Bar
derives fromboost::noncopyable
(even though it's not a direct inheritance), so yes.是的,如果它是可复制的,那么所有基类都必须是可复制的,但 boost::noncopyable 是不可复制的
Yes, if it were copyable then all base classes must be copyable, but boost::noncopyable is non-copyable