下面的snipplet是否適用於爲類定義所有其他方法生成的方法和構造函數?在類中正確使用`= delete`方法
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?
};
這將刪除每個默認的編譯器實現,只留下析構函數,對不對?如果沒有它,我猜可能會(幾乎)不可用,但我可以刪除它,對嗎?
返回類型Picture&
的移動分配operator=(Picture&&)
是否正確?如果我爲返回類型寫了Picture&&
,它會有所作爲嗎?
看起來好像C++在C++ 0x!中真的改變了很多,有趣的東西。 – briantyler 2011-04-16 14:19:55
這是一個更清晰的語法,然後將這些方法隱藏在「private」部分中,並且只聲明*但不定義*它們。 – towi 2011-05-27 08:03:39