1. 程式人生 > 其它 >C++學習記錄(九)智慧指標、異常處理

C++學習記錄(九)智慧指標、異常處理

這是第九天的學習。

  1 #include <iostream>
  2 #include <memory>
  3 
  4 using namespace std;
  5 class A
  6 {
  7     int id;
  8 public:
  9     A(int id) : id(id) { cout << "A" << endl; }
 10     ~A() { cout << "~A" << endl; }
 11 
 12     void showInfo() { cout << this
->id << endl; } 13 }; 14 15 template <class T> 16 class AutoPtr 17 { 18 private: 19 T* p; 20 public: 21 AutoPtr(T* _p) : p(_p) {} 22 ~AutoPtr() { delete p; } 23 24 T* get() { return this->p; } 25 AutoPtr(AutoPtr& autoptr) 26 { 27 this
->p = autoptr.p; 28 autoptr.p = nullptr; 29 } 30 }; 31 32 template<class T> 33 class Count // 定義一個計數器模板 34 { 35 private: 36 T* p; // 定義指標 37 int m_count; // 定義計數值 38 public: 39 Count(T* _p = nullptr) : p(_p)
40 { // 建構函式,當指標p不為空時,計數值為1 41 if(p != nullptr) m_count = 1; 42 cout << "Count" << endl; 43 } 44 ~Count() { cout << "~Count" << endl; } 45 46 // 計數器加法 47 void Add() { this->m_count++; } 48 // 計數器減法 49 int Sub() { return --m_count; } 50 int getCount() { return this->m_count; } 51 }; 52 53 template <class T> 54 class SharedPtr // 定義一個共享指標 55 { 56 private: 57 T* p; 58 //static int numb; // 靜態變數所有類共享,不安全 59 Count<T>* count; // 定義計數器 60 public: 61 SharedPtr(T* _p = nullptr) : p(_p) 62 { // 建構函式,新建計數器 63 if (p != nullptr) 64 { 65 count = new Count<T>(p); 66 } 67 } 68 //~SharedPtr() { if (numb == 0) delete p; else numb--; } 69 ~SharedPtr() 70 { 71 if (count->Sub() == 0) 72 { 73 delete this->p; 74 delete this->count; 75 p = nullptr; 76 count = nullptr; 77 } 78 //else numb--; 79 } 80 81 T* get() { return this->p; } 82 SharedPtr(const SharedPtr& sharedptr) : p(sharedptr.p),count(sharedptr.count) 83 { // 拷貝建構函式 84 if (p != nullptr) count->Add(); 85 //numb++; 86 cout << "copy constructor" << endl; 87 } 88 SharedPtr& operator=(const SharedPtr& sharedptr) 89 { 90 this->p = sharedptr.p; 91 this->count = sharedptr.count; 92 count->Add(); 93 return *this; 94 } 95 int useCount() { return count->getCount(); } 96 97 }; 98 99 //template <class T> 100 //int SharedPtr<T>::numb = 0; 101 102 int main() 103 { 104 // -1- 智慧指標,是一個類模板 105 // auto_ptr(棄用),shard_ptr(共享智慧指標),unqiue_ptr(唯一指標) 106 107 A* p1 = new A(123); 108 p1->showInfo(); 109 delete p1; 110 /* 111 auto_ptr<A> p2(new A(456)); // auto_ptr在C++17中被廢棄了 112 p2.get()->showInfo(); 113 114 auto_ptr<A> p3(p2); // 呼叫拷貝構造 115 p3.get()->showInfo(); 116 p2.get()->showInfo(); // p2賦值給p3後變為空指標 117 */ 118 /* 119 AutoPtr<A> p4(new A(789)); 120 p4.get()->showInfo(); 121 122 AutoPtr<A> p5(p4); 123 p5.get()->showInfo(); 124 */ 125 126 // 127 /* 128 shared_ptr<A> p6(new A(147)); 129 p6.get()->showInfo(); 130 shared_ptr<A> p7(p6); 131 p7.get()->showInfo(); 132 p6.get()->showInfo(); 133 */ 134 /* 135 SharedPtr<A> p8(new A(258)); 136 p8.get()->showInfo(); 137 SharedPtr<A> p9(p8); 138 p9.get()->showInfo(); 139 p8.get()->showInfo(); 140 */ 141 // 142 SharedPtr<A> ptr1(new A(100)); 143 SharedPtr<A> ptr2(ptr1); 144 SharedPtr<A> ptr3; 145 ptr3 = ptr2; 146 cout << " " << ptr3.useCount() << endl; 147 148 //cout << "Hello World!" << endl; 149 return 0; 150 }
 1 #include <iostream>
 2 #include <memory>
 3 
 4 using namespace std;
 5 class B;                // 前置宣告
 6 class A
 7 {
 8 public:
 9     weak_ptr<B> ptr_b;
10 public:
11     A() { cout << "A" << endl; }
12     ~A() { cout << "~A" << endl; }
13     void showInfo() { cout << "ShowInfo" << endl; }
14 };
15 class B
16 {
17 public:
18     weak_ptr<A> ptr_a;
19 public:
20     B() { cout << "B" << endl; }
21     ~B() { cout << "~B" << endl; }
22 
23     void func()
24     {
25         // 提升對物件的許可權
26         shared_ptr<A> temp = this->ptr_a.lock();
27         if( temp != nullptr )
28         {
29             temp->showInfo();
30         }
31     }
32 };
33 
34 int main()
35 {
36     // weak_ptr,沒有物件的訪問權
37     // 定義物件時用shared_ptr,用new,否則用weak_ptr
38     shared_ptr<A> pA(new A);
39     shared_ptr<B> pB(new B);
40 
41     pA->ptr_b = pB;
42     pB->ptr_a = pA;
43 
44     cout << pA.use_count() << endl;
45     cout << pB.use_count() << endl;
46 
47     cout << "Hello World!" << endl;
48     return 0;
49 }
 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 //class Error
 6 //{
 7 //private:
 8 //    string errorMsg;
 9 //public:
10 //    Error(string errorMsg) { this->errorMsg = errorMsg; }
11 //    string What() { return this->errorMsg; }
12 
13 //};
14 //Error err("b must not be 0");
15 logic_error err("b must not be 0");
16 
17 float fun_div(int a, int b)
18 {
19     if ( b==0 )
20     {
21         //throw -1;
22         throw err;
23     }
24     return a/b;
25 }
26 int fun_add(int a, int b)
27 {
28     return fun_div(a,b) + a + b;
29 }
30 int fun_mul(int a, int b)
31 {
32     return fun_add(a,b) * a * b;
33 }
34 
35 int main()
36 {
37     // 面向異常處理機制的框架
38     // throw關鍵字:丟擲異常, throw + 類物件 或基本型別的值
39     // 捕捉異常try{...丟擲異常的闡述...}catch()
40     // 處理異常catch(異常數型 異常的物件或變數){...異常處理語句塊...}
41     int data;
42     int a = 10;
43     int b = 0;
44     try
45     {
46         data = fun_mul(a,b);
47         cout << "final data is " << data << endl;
48     }
49     /*
50     catch (int e)
51     {
52         if ( e== -1 ) { cout << "b must not be 0" << endl; }
53     }
54     */
55     /*
56     catch (Error& e)
57     {
58         cout << e.What() << endl;
59     }
60     */
61     catch (logic_error& e)
62     {
63         cout << e.what() << endl;
64     }
65 
66     cout << "Hello World!" << endl;
67     return 0;
68 }