日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

C++ Primer 读书笔记 - 第十三章

發(fā)布時(shí)間:2023/11/27 生活经验 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++ Primer 读书笔记 - 第十三章 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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. 為了防止復(fù)制,我們可以把拷貝構(gòu)造函數(shù)設(shè)置為private,同時(shí)只在類中聲明,但不在任何地方定義。

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. 編譯器會為我們生成一個(gè)拷貝構(gòu)造函數(shù),但是一旦我們自己來寫拷貝構(gòu)造函數(shù),那么我們必須對每個(gè)成員變量顯示賦值,否則成員變量由其自身的默認(rèn)構(gòu)造函數(shù)來賦值。

8. 簡單的Smart pointer示例。

#include <iostream>
using std::ostream; using std::cout; using std::endl;
#include <string>
#include <cstddef>
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() << "\tval: " << 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

? ??構(gòu)造函數(shù)和析構(gòu)函數(shù)

轉(zhuǎn)載于:https://www.cnblogs.com/null00/archive/2013/05/31/3107928.html

總結(jié)

以上是生活随笔為你收集整理的C++ Primer 读书笔记 - 第十三章的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。