C++ 帶有指針成員的類處理方式


在一個類中,如果類沒有指針成員,一切方便,因為默認合成的析構函數會自動處理所有的內存。但是如果一個類帶了指針成員,那么需要我們自己來寫一個析構函數來管理內存。在<<c++ primer>> 中寫到,如果一個類需要我們自己寫析構函數,那么這個類,也會需要我們自己寫拷貝構造函數和拷貝賦值函數。

析構函數:

我們這里定義一個類HasPtr,這個類中包含一個int 類型的指針。然后定義一個析構函數,這個函數打印一句話。

HasPtr.h 類的頭文件

 1 #pragma once
 2 #ifndef __HASPTR__
 3 #define __HASPTR__
 4 
 5 class HasPtr
 6 {
 7 public:
 8     HasPtr(int i,int *p);
 9     //HasPtr& operator=(HasPtr&);
10     //HasPtr(const HasPtr&);
11     ~HasPtr();
12     int get_ptr_value();
13     void set_ptr_value(int *p);
14     int get_val();
15     void set_val(int v);
16 private:
17     int val;
18     int *ptr;
19 };
20 
21 #endif // !__HASPTR__
View Code

 

HasPtr.cpp 類的實現

 1 #include "stdafx.h"
 2 
 3 #include <iostream>
 4 #include "HasPtr.h"
 5 
 6 using namespace std;
 7 
 8 HasPtr::HasPtr(int i, int *p)
 9 {
10     val = i;
11     ptr = p;
12 }
13 
14 int HasPtr::get_ptr_value()
15 {
16     return *ptr;
17 }
18 
19 void HasPtr::set_ptr_value(int *p)
20 {
21     ptr = p;
22 }
23 
24 int HasPtr::get_val()
25 {
26     return val;
27 }
28 
29 void HasPtr::set_val(int v)
30 {
31     val = v;
32 }
33 
34 HasPtr::~HasPtr()
35 {
36     cout << "destructor of HasPtr " << endl;
37 }
View Code

 

ClassWithPointer 類,包含main入口,HasPtr在stack上。

 1 // ClassWithPointer.cpp : 定義控制台應用程序的入口點。
 2 //
 3 
 4 #include "stdafx.h"
 5 #include <iostream>
 6 #include "HasPtr.h"
 7 using namespace std;
 8 
 9 int main()
10 {
11     int temp = 100;
12     HasPtr ptr(2,&temp);
13     cout << ptr.get_ptr_value() << endl;
14     cout << ptr.get_val() << endl;
15     system("PAUSE");
16     system("PAUSE");
17     return 0;
18 }
View Code

執行該入口方法,發現最后還是打印了析構函數這句話,OK,在main 方法中,stack上定義了一個HasPtr,在main方法退出前,析構函數自動調用了。

如果將HasPtr改為動態對象,也就是放在堆上呢? 

ClassWithPointer 類,包含main入口,HasPtr在heap上。

 1 // ClassWithPointer.cpp : 定義控制台應用程序的入口點。
 2 //
 3 
 4 #include "stdafx.h"
 5 #include <iostream>
 6 #include "HasPtr.h"
 7 using namespace std;
 8 
 9 int main()
10 {
11     int temp = 100;
12     //HasPtr ptr(2,&temp);
13     HasPtr *ptr = new HasPtr(2,&temp);
14     cout << ptr->get_ptr_value() << endl;
15     cout << ptr->get_val() << endl;
16     system("PAUSE");
17     return 0;
18 }
View Code

 

執行一下,發現析構函數沒有調用。OK,我們在return 0前面添加一個delete ptr; 析構函數執行了。

所以,這里有兩個結論:

  1. 當一個對象在stack 上時,析構函數自動調用。
  2. 當一個對象在heap上時,需要調用delete 語句,析構函數才會被執行。

現在在析構函數中調用delete 語句來刪除指針成員。

頭文件不變,HasPtr.cpp 文件代碼如下:

 1 #include "stdafx.h"
 2 
 3 #include <iostream>
 4 #include "HasPtr.h"
 5 
 6 using namespace std;
 7 
 8 HasPtr::HasPtr(int i, int *p)
 9 {
10     val = i;
11     ptr = p;
12 }
13 
14 int HasPtr::get_ptr_value()
15 {
16     return *ptr;
17 }
18 
19 void HasPtr::set_ptr_value(int *p)
20 {
21     ptr = p;
22 }
23 
24 int HasPtr::get_val()
25 {
26     return val;
27 }
28 
29 void HasPtr::set_val(int v)
30 {
31     val = v;
32 }
33 
34 HasPtr::~HasPtr()
35 {
36     cout << "destructor of HasPtr " << endl;
37     delete ptr;
38 }
View Code

 ClassWithPointer 代碼如下:

 1 // ClassWithPointer.cpp : 定義控制台應用程序的入口點。
 2 //
 3 
 4 #include "stdafx.h"
 5 #include <iostream>
 6 #include "HasPtr.h"
 7 using namespace std;
 8 
 9 int main()
10 {
11     int temp = 100;
12     HasPtr ptr(2,&temp);
13     cout << ptr.get_ptr_value() << endl;
14     cout << ptr.get_val() << endl;
15     system("PAUSE");
16     return 0;
17 }
View Code

 

執行一下,正常打印結束后,拋出錯誤:

這里說明delete 不能刪除stack 上的指針值。

現在在ClassWithPointer傳入一個動態指針來測試一下。

 1 // ClassWithPointer.cpp : 定義控制台應用程序的入口點。
 2 //
 3 
 4 #include "stdafx.h"
 5 #include <iostream>
 6 #include "HasPtr.h"
 7 using namespace std;
 8 
 9 int main()
10 {
11     int *temp = new int(100);
12     HasPtr ptr(2,temp);
13     cout << ptr.get_ptr_value() << endl;
14     cout << ptr.get_val() << endl;
15     system("PAUSE");
16     return 0;
17 }
View Code

 

執行后析構函數正常運行。所以這里有兩個結論:

  1. delete 語句不能刪除stack 上的指針值。
  2. delete 語句只能刪除heap上的指針值,也就是new 出來的對象。

 

默認拷貝構造函數和默認賦值操作:

這里我們調用默認的構造函數和默認的賦值操作,看看會出現什么,為了方便查看,我在析構函數中打印了當前對象的地址,以及在main方法中打印了對象地址,這樣就可以看到哪個對象調用了析構函數:

HasPtr.cpp 代碼如下:

 1 #include "stdafx.h"
 2 
 3 #include <iostream>
 4 #include "HasPtr.h"
 5 
 6 using namespace std;
 7 
 8 HasPtr::HasPtr(int i, int *p)
 9 {
10     val = i;
11     ptr = p;
12 }
13 
14 int HasPtr::get_ptr_value()
15 {
16     return *ptr;
17 }
18 
19 void HasPtr::set_ptr_value(int *p)
20 {
21     ptr = p;
22 }
23 
24 int HasPtr::get_val()
25 {
26     return val;
27 }
28 
29 void HasPtr::set_val(int v)
30 {
31     val = v;
32 }
33 
34 HasPtr::~HasPtr()
35 {
36     cout << "destructor of HasPtr " << this << endl;
37     delete ptr;
38 }
View Code

 

ClassWithPointer 代碼如下:

 1 // ClassWithPointer.cpp : 定義控制台應用程序的入口點。
 2 //
 3 
 4 #include "stdafx.h"
 5 #include <iostream>
 6 #include "HasPtr.h"
 7 using namespace std;
 8 
 9 int main()
10 {
11     int *temp = new int(100);
12     HasPtr ptr(2,temp);
13     cout << "ptr-------------->" << &ptr << endl;
14     cout << ptr.get_ptr_value() << endl;
15     cout << ptr.get_val() << endl;
16     
17     HasPtr ptr2(ptr);
18     cout << "ptr2-------------->" << &ptr2 << endl;
19     cout << ptr2.get_ptr_value() << endl;
20     cout << ptr2.get_val() << endl;
21     
22     HasPtr ptr3 = ptr;
23     cout << "ptr3-------------->" << &ptr3 << endl;
24     cout << ptr3.get_ptr_value() << endl;
25     cout << ptr3.get_val() << endl;
26 
27     system("PAUSE");
28     return 0;
29 }
View Code

 

運行結果如下,最后還是報錯了:

 

其實程序運行到第二個析構函數時,報錯了。報錯原因是,ptr 其實已經是pending指針了,因為這個ptr 指針所指向的地址已經被delete了。

不過我們這里最起碼可以知道默認的拷貝構造函數和賦值操作,也是會直接復制指針值的,不是指針所指向的值。是指針變量的值,也就是地址。

所以這里引申出來的問題是:如何管理對象中指針成員的內存? 這個是一個核心問題。

上面的例子,就是默認的方式,但是管理失敗了,因為析構函數到最后會刪除pending 指針,導致異常發生。

 

智能指針:

引入一個類U_Ptr,用來管理我們需要在業務對象中需要的指針變量,假設為int *p。頭文件如下:

 1 #pragma once
 2 #ifndef __UPTR__
 3 #define __UPTR__
 4 #include "HasPtr.h"
 5 #include <iostream>
 6 
 7 using namespace std;
 8 class U_Ptr
 9 {
10     friend class HasPtr;
11     int *ip;
12     size_t use;
13 
14     U_Ptr(int *p):ip(p),use(1) {}
15     ~U_Ptr() 
16     {
17         cout << "destruction:"<< *ip << endl;
18         delete ip;
19     }
20 };
21 #endif // !__UPTR__
View Code

現在我們的業務對象還是HasPtr。頭文件如下:

 1 #pragma once
 2 #ifndef __HASPTR__
 3 #define __HASPTR__
 4 #include "U_Ptr.h"
 5 class HasPtr
 6 {
 7 public:
 8     HasPtr(int *p, int i):ptr(new U_Ptr(p)),val(i){}
 9 
10     HasPtr(const HasPtr &orgi) :ptr(orgi.ptr), val(orgi.val) 
11     {
12         ++ptr->use; 
13         cout << "coming into copy construction:" << ptr->use << endl;
14     }
15 
16     HasPtr& operator=(const HasPtr &rhs);
17 
18     ~HasPtr();
19 
20     int get_ptr_value() const;
21     int get_int() const;
22     void set_ptr(int *p);
23     void set_int(int i);
24 private:
25     U_Ptr *ptr;
26     int val;
27 };
28 
29 #endif // !__HASPTR__
View Code

 

HasPtr.cpp 實現如下:

 1 #include "stdafx.h"
 2 #include "HasPtr.h"
 3 #include <iostream>
 4 
 5 using namespace std;
 6 
 7 HasPtr& HasPtr::operator=(const HasPtr &rhs)
 8 {
 9     ++rhs.ptr->use;
10     if (--ptr->use == 0)
11     {
12         delete ptr;
13     }
14     ptr = rhs.ptr;
15     val = rhs.val;
16     return *this;
17 }
18 
19 HasPtr::~HasPtr()
20 {
21     cout << "destruction:" << ptr->use << endl;
22     if (--ptr->use == 0)
23     {
24         delete ptr;
25     }
26 }
27 
28 int HasPtr::get_ptr_value() const
29 {
30     return *ptr->ip;
31 }
32 int HasPtr::get_int() const
33 {
34     return val;
35 }
36 void HasPtr::set_ptr(int *p)
37 {
38     ptr->ip = p;
39 }
40 void HasPtr::set_int(int i)
41 {
42     val = i;
43 }
View Code

 

測試類如下:

 1 // SmartPointer.cpp : 定義控制台應用程序的入口點。
 2 //
 3 
 4 #include "stdafx.h"
 5 #include "HasPtr.h"
 6 #include <iostream>
 7 
 8 using namespace std;
 9 
10 
11 int main()
12 {
13     int *temp = new int(100);
14     HasPtr ptr(temp,22);
15     cout << "ptr------------>" << endl;
16     cout << ptr.get_ptr_value() << endl;
17     cout << ptr.get_int() << endl;
18     HasPtr ptr2(ptr);
19     cout << "ptr2------------>" << endl;
20     cout << ptr2.get_ptr_value() << endl;
21     cout << ptr2.get_int() << endl;
22     system("PAUSE");
23     return 0;
24 }
View Code

 

 

我們把U_Ptr 叫做智能指針,用於幫我們管理需要的指針成員。我們的業務對象HasPtr對象包含一個智能指針,這個指針在HasPtr 對象創建時創建,智能指針的use 變量用來記錄業務對象HasPtr對象被復制了多少次,也就是說,有多少個相同的指針指向了ptr所指向的地方。如果要記錄HasPtr對象一共有多少個一樣的,那么就需要在拷貝構造函數和賦值操作處進行對use變量加一操作,在析構函數處進行減一操作。當減到0時,刪除指針。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM