隨着移動構造函數的出現,它現在變成了六大類。在這裏學習和理解所有的細節,並且你將在課堂鍋爐板上獲得碩士學位。
#include <list>
class A {};
class B {};
class C{
std::list<A> a;
std::list<B> b;
public:
typedef std::list<A>::size_type size_type;
explicit C(size_type sz =0);
virtual ~C();
C(const C& c);
// Specialize external swap. Necessary for assignment operator below,
// and for ADL (argument-dependant lookup).
friend void swap(C& first, C& second);
// Assignment-operator. Note that the argument "other" is passed by value.
// This is the copy-and-swap idiom (best practice).
C& operator=(C other); // NOTE WELL. Passed by value
// move-constructor - construct-and-swap idiom (best practice)
C(C&& other);
};
C::C(size_type sz) : a(sz), b(sz) {}
C::~C(){}
C::C(const C& c) :a(c.a), b(c.b){}
void swap(C& first, C& second) {
// enable ADL (best practice)
using std::swap;
swap(first.a, second.a);
swap(first.b, second.b);
}
// Assignment-operator. Note that the argument "other" is passed by value.
// This is the copy-and-swap idiom (best practice).
C& C::operator=(C other) {
swap(*this, other); // Uses specialized swap above.
return *this;
}
// move-constructor - construct-and-swap idiom (best practice)
C::C(C&& other): a(0) , b(0) {
swap(*this, other);
}
這是什麼'list','std :: list'? –
nope,它不是作業,編輯後 - 是std :: list。 –
你能詳細說明你的意思嗎:*包含兩個其他類的列表*? 這實際上是兩個其他兩個類*的實例列表嗎?每個列表是否需要包含其他類的對象或只是一種類型對象的容器? – marko