【发布时间】:2011-08-06 22:06:28
【问题描述】:
对于取消定义一个类的所有其他生成的方法和构造函数,以下片段是否正确?
struct Picture {
// 'explicit': no accidental cast from string to Picture
explicit Picture(const string &filename) { /* load image from file */ }
// no accidental construction, i.e. temporaries and the like
Picture() = delete;
// no copy
Picture(const Picture&) = delete;
// no assign
Picture& operator=(const Picture&) = delete;
// no move
Picture(Picture&&) = delete;
// no move-assign
Picture& operator=(Picture&&) = delete; // return type correct?
};
这会删除所有默认编译器实现,只留下析构函数,对吗?如果没有它,我猜这个类将(几乎)无法使用,但我也可以删除它,对吗?
move-assign operator=(Picture&&) 的返回类型Picture& 是否正确?如果我为返回类型写 Picture&& 会有所不同吗?
【问题讨论】:
-
在 C++0x 中,C++ 似乎真的发生了很大的变化!,有趣的东西。
-
它的语法比将这些方法隐藏在
private部分中的常见做法更清晰,并且只声明而不是定义它们。
标签: c++ c++11 operator-overloading move-semantics rvalue-reference