1. Initialization和Assignment不一样。其中Initialization包括direct-initialization (如A a(...))和copy-initialization (如 A a = b;)
注意A a = b为copy-initialization,
而A a; A b; a = b;为Assignment。
2. We cannot copy objects of the IO types, so we cannot use copy-initialization on objects of these types.
3. As the copy constructor is used (implicitly) to pass and return objects to and from objects, it usually should not be made explicit.
4. 为了防止复制,我们可以把拷贝构造函数设置为private,同时只在类中声明,但不在任何地方定义。
5. Rule of Three, if you need a destructor, then you need all three copy-control members.
6. Even if we write our own destructor, the synthesized destructor is still run.
7. 编译器会为我们生成一个拷贝构造函数,但是一旦我们自己来写拷贝构造函数,那么我们必须对每个成员变量显示赋值,否则成员变量由其自身的默认构造函数来赋值。
8. 简单的Smart pointer示例。
#includeusing std::ostream; using std::cout; using std::endl; #include <string> #include using std::size_t; /* smart pointer class: takes ownership of the dynamically allocated* object to which it is bound * User code must dynamically allocate an object to initialize a HasPtr* and must not delete that object; the HasPtr class will delete it */ //private class for use by HasPtr only class U_Ptr {friend class HasPtr;int *ip;size_t use;U_Ptr(int *p): ip(p), use(1) { }~U_Ptr() { cout << "U_Ptr destructor" << endl; delete ip; } };class HasPtr { public:// HasPtr owns the pointer; p must have been dynamically allocatedHasPtr(int *p, int i): ptr(new U_Ptr(p)), val(i) { }// copy members and increment the use countHasPtr(const HasPtr &orig):ptr(orig.ptr), val(orig.val) { ++ptr->use; }HasPtr& operator=(const HasPtr&);// if use count goes to zero, delete the U_Ptr object~HasPtr() { if (--ptr->use == 0) {cout << "HasPtr destructor" << endl;delete ptr; }} friend ostream& operator<<(ostream&, const HasPtr&);// copy control and constructors as before// accessors must change to fetch value from U_Ptr objectint *get_ptr() const { return ptr->ip; } int get_int() const { return val; }// change the appropriate data membervoid set_ptr(int *p) { ptr->ip = p; }void set_int(int i) { val = i; }// return or change the value pointed to, so ok for const objects// Note: *ptr->ip is equivalent to *(ptr->ip)int get_ptr_val() const { return *ptr->ip; } void set_ptr_val(int i) { *ptr->ip = i; } private:U_Ptr *ptr; // points to use-counted U_Ptr classint val; };HasPtr& HasPtr::operator=(const HasPtr &rhs) {++rhs.ptr->use; // increment use count on rhs firstif (--ptr->use == 0) {cout << "operator= delete" << endl;delete ptr; // if use count goes to 0 on this object, delete it }ptr = rhs.ptr; // copy the U_Ptr objectval = rhs.val; // copy the int memberreturn *this; }ostream& operator<<(ostream &os, const HasPtr &hp) {os << "*ptr: " << hp.get_ptr_val() << " val: " << hp.get_int() << endl;return os; }int main() {int *obj = new int(0);HasPtr ptr1(obj, 42);HasPtr ptr2(ptr1);cout << "(1) ptr1: " << ptr1 << endl << "ptr2: " << ptr2 << endl;ptr1.set_ptr_val(42); // sets object to which both ptr1 and ptr2 pointptr2.get_ptr_val(); // returns 42 cout << "(2) ptr1: " << ptr1 << endl << "ptr2: " << ptr2 << endl;ptr1.set_int(0); // changes s member only in ptr1ptr2.get_int(); // returns 42ptr1.get_int(); // returns 0 cout << "(3) ptr1: " << ptr1 << endl << "ptr2: " << ptr2 << endl; }
9. Define Valuelike Class
构造函数和析构函数