服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - C++带有指针成员的类处理方式详解

C++带有指针成员的类处理方式详解

2021-05-29 18:45^~~^ C/C++

这篇文章主要为大家详细介绍了C++带有指针成员的类处理方式,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

在一个类中,如果类没有指针成员,一切方便,因为默认合成的析构函数会自动处理所有的内存。但是如果一个类带了指针成员,那么需要我们自己来写一个析构函数来管理内存。在<<c++ primer>> 中写到,如果一个类需要我们自己写析构函数,那么这个类,也会需要我们自己写拷贝构造函数和拷贝赋值函数。

析构函数:

我们这里定义一个类HasPtr,这个类中包含一个int 类型的指针。然后定义一个析构函数,这个函数打印一句话。

HasPtr.h 类的头文件

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

HasPtr.cpp 类的实现

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

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

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

执行该入口方法,发现最后还是打印了析构函数这句话,OK,在main 方法中,stack上定义了一个HasPtr,在main方法退出前,析构函数自动调用了。

如果将HasPtr改为动态对象,也就是放在堆上呢?

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

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

执行一下,发现析构函数没有调用。OK,我们在return 0前面添加一个delete ptr; 析构函数执行了。

所以,这里有两个结论:

  • 当一个对象在stack 上时,析构函数自动调用。
  • 当一个对象在heap上时,需要调用delete 语句,析构函数才会被执行。

现在在析构函数中调用delete 语句来删除指针成员。

头文件不变,HasPtr.cpp 文件代码如下:

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

 ClassWithPointer 代码如下:

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

执行一下,正常打印结束后,抛出错误:

C++带有指针成员的类处理方式详解

这里说明delete 不能删除stack 上的指针值。

现在在ClassWithPointer传入一个动态指针来测试一下。

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

执行后析构函数正常运行。所以这里有两个结论:

  • delete 语句不能删除stack 上的指针值。
  • delete 语句只能删除heap上的指针值,也就是new 出来的对象。 

默认拷贝构造函数和默认赋值操作:

这里我们调用默认的构造函数和默认的赋值操作,看看会出现什么,为了方便查看,我在析构函数中打印了当前对象的地址,以及在main方法中打印了对象地址,这样就可以看到哪个对象调用了析构函数:

HasPtr.cpp 代码如下:

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

ClassWithPointer 代码如下:

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

运行结果如下,最后还是报错了:

C++带有指针成员的类处理方式详解

其实程序运行到第二个析构函数时,报错了。报错原因是,ptr 其实已经是pending指针了,因为这个ptr 指针所指向的地址已经被delete了。

不过我们这里最起码可以知道默认的拷贝构造函数和赋值操作,也是会直接复制指针值的,不是指针所指向的值。是指针变量的值,也就是地址。

所以这里引申出来的问题是:如何管理对象中指针成员的内存? 这个是一个核心问题。

上面的例子,就是默认的方式,但是管理失败了,因为析构函数到最后会删除pending 指针,导致异常发生。 

智能指针:

引入一个类U_Ptr,用来管理我们需要在业务对象中需要的指针变量,假设为int *p。头文件如下:

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

现在我们的业务对象还是HasPtr。头文件如下:

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

HasPtr.cpp 实现如下:

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

测试类如下:

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

我们把U_Ptr 叫做智能指针,用于帮我们管理需要的指针成员。我们的业务对象HasPtr对象包含一个智能指针,这个指针在HasPtr 对象创建时创建,智能指针的use 变量用来记录业务对象HasPtr对象被复制了多少次,也就是说,有多少个相同的指针指向了ptr所指向的地方。如果要记录HasPtr对象一共有多少个一样的,那么就需要在拷贝构造函数和赋值操作处进行对use变量加一操作,在析构函数处进行减一操作。当减到0时,删除指针。

原文链接:http://www.cnblogs.com/lucy-lizhi/p/6551308.html

延伸 · 阅读

精彩推荐
  • C/C++C语言实现双人五子棋游戏

    C语言实现双人五子棋游戏

    这篇文章主要为大家详细介绍了C语言实现双人五子棋游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    两片空白7312021-11-12
  • C/C++OpenCV实现拼接图像的简单方法

    OpenCV实现拼接图像的简单方法

    这篇文章主要为大家详细介绍了OpenCV实现拼接图像的简单方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    iteye_183805102021-07-29
  • C/C++使用C++制作简单的web服务器(续)

    使用C++制作简单的web服务器(续)

    本文承接上文《使用C++制作简单的web服务器》,把web服务器做的功能稍微强大些,主要增加的功能是从文件中读取网页并返回给客户端,而不是把网页代码...

    C++教程网5492021-02-22
  • C/C++深入C++拷贝构造函数的总结详解

    深入C++拷贝构造函数的总结详解

    本篇文章是对C++中拷贝构造函数进行了总结与介绍。需要的朋友参考下...

    C++教程网5182020-11-30
  • C/C++C语言main函数的三种形式实例详解

    C语言main函数的三种形式实例详解

    这篇文章主要介绍了 C语言main函数的三种形式实例详解的相关资料,需要的朋友可以参考下...

    ieearth6912021-05-16
  • C/C++关于C语言中E-R图的详解

    关于C语言中E-R图的详解

    今天小编就为大家分享一篇关于关于C语言中E-R图的详解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看...

    Struggler095962021-07-12
  • C/C++c/c++实现获取域名的IP地址

    c/c++实现获取域名的IP地址

    本文给大家汇总介绍了使用c/c++实现获取域名的IP地址的几种方法以及这些方法的核心函数gethostbyname的详细用法,非常的实用,有需要的小伙伴可以参考下...

    C++教程网10262021-03-16
  • C/C++c/c++内存分配大小实例讲解

    c/c++内存分配大小实例讲解

    在本篇文章里小编给大家整理了一篇关于c/c++内存分配大小实例讲解内容,有需要的朋友们可以跟着学习参考下。...

    jihite5172022-02-22