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 }

 

posted @ 2022-05-13 22:05  Z_He  阅读(31)  评论(0编辑  收藏  举报