copy-and-swap

    0熱度

    5回答

    我有一個類有兩個向量:int和Str。現在我想定義一個拷貝構造函數,以便元素順序顛倒過來;例如如果a=(1,Hello),(2,World)和我寫auto b=a;我得到b=(2,world),(1,hello)。這是完美的工作。我遇到的問題是超載運算符=,以便使用複製構造函數。這裏是我的班加拷貝構造函數: class grade { private: static int

    12熱度

    2回答

    閱讀this有關複製和交換成語我讀過this這下(2)後說: class_name & class_name :: operator= (const class_name &) (2) (2)典型聲明一個拷貝賦值運算符時 複製和交換成語不能使用 我們什麼時候應該避免使用複製和交換成語? 而當它「完全不能使用」? 是否存在複製 - 交換和零規則不適用的現實生活案例? 我的確發現了這個ques

    0熱度

    1回答

    我正在使用PHP的數組,我試圖交換數組的項目。我有這樣的代碼: <html> <body> <p> <?php $test2 = array ( array("!","#","#"), array("@","!","#"), array("@","@","!",) ); for($f=0; $f < count(

    2熱度

    1回答

    我有一個類 class MyClass { public : int a; int b; } 對於使用副本交換成語然後我創建函數 void MyClass::swap(MyClass& other) { std::swap(a,other.a); std::swap(b,other.b); } 如果,以後,我改變我的等級和刪除成員a,那麼編譯器會在swap

    21熱度

    2回答

    通過使用Copy & Swap成語,我們可以很容易地實現與強異常安全的拷貝賦值: T& operator = (T other){ using std::swap; swap(*this, other); return *this; } 然而,這需要T是Swappable。如果std::is_move_constructible_v<T> && std::is_

    0熱度

    1回答

    試圖給出這個問題的答案text-file-handling-in-c給引用cplusplus.com。我遇到了std::swap功能的流類型,如fstream。 所以我的問題是: 到底是什麼的交換功能的目的,例如對於'fstream'分別在哪種情況下我必須使用它? 引用到Q & A C++ std::ifstream in constructor problem我知道流類型是不可複製的。參照Q &

    0熱度

    3回答

    在使用複製和交換方法時,我有一個關於賦值運算符的問題。 String & operator = (String s) // the pass-by-value parameter serves as a temporary { s.swap (*this); // Non-throwing swap return *this; }// Old resources relea

    4熱度

    1回答

    我有模板類節點,以遵循copy-and-swap成語我是tryig寫swap函數,它是類的friendNode 我的代碼是: Node.h #ifndef NODE_H #define NODE_H #include<memory> template<typename type> class Node { type data_; std::unique_ptr<No

    -3熱度

    2回答

    當我嘗試在抽象類中使用複製 - 交換成語時遇到了一個有趣的錯誤。下面的代碼是專門創建演示這種情況下,如果有什麼不正確的,告訴我,但不集中: template <typename T> class iter { public: iter(); virtual ~iter(); iter(const iter &); iter(iter &&) noexce

    1熱度

    2回答

    最近,我經常通過爲operator=分配一個新值來重置對象。我的大多數課程都使用「複製和交換」慣用語定義了copy constructor和operator=。在大多數情況下,它工作正常,雖然效率不如以前,但大多並不重要。有一種情況下,這不起作用。它的時候需要在新對象的constructor之前調用destructor。 注:最讓我使用這個類是不可複製 class Foo { public: