C++学习笔记基础篇25——拷贝构造函数&析构函数冲突问题:重载函数&重载命令符

拷贝构造函数

依然使用Student作为例子展开,其中CStudent(CStudent& stud) 就是拷贝构造函数,可以实现在使用类去初始话一个对象时,用同一个类的另外一个对象初始化。

class CStudent
{
public:
	char *p_name;
	char sex;
	int num;
	int age;
	CStudent(){};
	CStudent(char* pname, char t_sex, int t_num, int t_age) :sex(t_sex), num(t_num), age(t_age)
	{
		p_name = NULL;
		int n_len = 0;
		if (pname)
		{
			n_len = strlen(pname);
		}

		if (n_len > 0)
		{
			p_name = new char[n_len + 1];
			memset(p_name, 0, n_len + 1);
			strcpy(p_name, pname);
		}
	}
	CStudent(CStudent& stud)
	{
		strcpy(p_name, stud.p_name);
		num = stud.num;
		age = stud.age;
	}
	~CStudent();
};

异常报错

在拷贝构造函数,加入对对应的析构函数后,程序如下所示,运行以下程序。

#include <iostream>
#include <string>
using namespace std;


class CStudent
{
public:
	char *p_name;
	char sex;
	int num;
	int age;
	CStudent(){};
	CStudent(char* pname, char t_sex, int t_num, int t_age) :sex(t_sex), num(t_num), age(t_age)
	{
		p_name = NULL;
		int n_len = 0;
		if (pname)
		{
			n_len = strlen(pname);
		}

		if (n_len > 0)
		{
			p_name = new char[n_len + 1];
			memset(p_name, 0, n_len + 1);
			strcpy(p_name, pname);
		}
	}
	CStudent(CStudent& stud)
	{
		strcpy(p_name, stud.p_name);
		num = stud.num;
		age = stud.age;
	}
	~CStudent();
};
CStudent::~CStudent()
{
	if (p_name)
	{
		delete[] p_name;
		p_name = NULL;
	}
}

void test()
{
	CStudent stud_1 = { "zhangsan",'m', 1001, 20 };
	CStudent stud_2(stud_1);
}

int main(int argc, char* argv[])
{
	test();
	return 0;
}

可能会出现以下报错
HEAP CORRUPTION DETECTED:before Free block(#0) at 0x008A4930.CRT detected that the application wrote to memory before start of heap buffer.
HEAP CORRUPTION DETECTED:before Free block(#0) at 0x008A4930.CRT detected that the application wrote to memory before start of heap buffer.
_BLOCK_TYPE_IS_VALID(PhEAD->nBlockUse
_BLOCK_TYPE_IS_VALID(PhEAD->nBlockUse)

由于出现问题,程序停止正常工作。windows将关闭程序,并会在有可用的解决方案时通知你。由于出现问题,程序停止正常工作。windows将关闭程序,并会在有可用的解决方案时通知你。
原因:拷贝构造函数的p_name指针和源构造函数的p_name指针指向的是同一块内存,当第一次析构之后~CStudent已经完成析构,但是由于有拷贝构造函数的p_name指针也需要析构,就会出现异常。

解决方案

解决方案就是在构建拷贝构造函数时,需要为新的指针开辟内存空间,防止两个指针指向同一块内存空间。

#include <iostream>
#include <string>
using namespace std;


class CStudent
{
public:
	char *p_name;
	char sex;
	int num;
	int age;
	CStudent(){};
	CStudent(char* pname, char t_sex, int t_num, int t_age) :sex(t_sex), num(t_num), age(t_age)
	{
		p_name = NULL;
		int n_len = 0;
		if (pname)
		{
			n_len = strlen(pname);
		}

		if (n_len > 0)
		{
			p_name = new char[n_len + 1];
			memset(p_name, 0, n_len + 1);
			strcpy(p_name, pname);
		}
	}
	CStudent(const CStudent& stud)
	{
		int n_len = 0;
		n_len = strlen(stud.p_name);
		p_name = new char[n_len + 1];
		memset(p_name, 0, n_len + 1);
		strcpy(p_name, stud.p_name);
		num = stud.num;
		age = stud.age;
	}
	~CStudent();
};
CStudent::~CStudent()
{
	if (p_name)
	{
		delete[] p_name;
		p_name = NULL;
	}
}

void test()
{
	CStudent stud_1 = { "zhangsan",'m', 1001, 20 };
	CStudent stud_2(stud_1);
}

int main(int argc, char* argv[])
{
	test();
	return 0;
}

上面是使用重载构造函数解决问题,如果想使用以下命令去实现初始化

CStudent stud_2;
stud_2 = stud_1;

仍然使用上诉程序是会报和之前一样的错误,因为之前的幅值是函数赋值,现在是命令符赋值,这里就需要使用重载运算符。

CStudent& operator=(const CStudent& stud)
	{
		int n_len = 0;
		n_len = strlen(stud.p_name);
		p_name = new char[n_len + 1];
		memset(p_name, 0, n_len + 1);
		strcpy(p_name, stud.p_name);
		num = stud.num;
		age = stud.age;
		return *this;
	}

完整代码如下:

#include <iostream>
#include <string>
using namespace std;


class CStudent
{
public:
	char *p_name;
	char sex;
	int num;
	int age;
	static int master;
	CStudent(){};
	CStudent(char* pname, char t_sex, int t_num, int t_age) :sex(t_sex), num(t_num), age(t_age)
	{
		p_name = NULL;
		int n_len = 0;
		if (pname)
		{
			n_len = strlen(pname);
		}

		if (n_len > 0)
		{
			p_name = new char[n_len + 1];
			memset(p_name, 0, n_len + 1);
			strcpy(p_name, pname);
		}
	}
	CStudent(const CStudent& stud)
	{
		int n_len = 0;
		n_len = strlen(stud.p_name);
		p_name = new char[n_len + 1];
		memset(p_name, 0, n_len + 1);
		strcpy(p_name, stud.p_name);
		num = stud.num;
		age = stud.age;
	}
	CStudent& operator=(const CStudent& stud)
	{
		int n_len = 0;
		n_len = strlen(stud.p_name);
		p_name = new char[n_len + 1];
		memset(p_name, 0, n_len + 1);
		strcpy(p_name, stud.p_name);
		num = stud.num;
		age = stud.age;
		return *this;
	}
	~CStudent();
};
CStudent::~CStudent()
{
	if (p_name)
	{
		delete[] p_name;
		p_name = NULL;
	}
}

int CStudent::master = 0;
void test()
{

	CStudent stud_1 = { "zhangsan",'m', 1001, 20 };
	CStudent stud_2;
	stud_2 = stud_1;
}

int main(int argc, char* argv[])
{
	
	test();
	return 0;
}
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
C++中,类的构造函数析构函数是用来初始化和销毁对象的函数。在类中,构造函数析构函数的名称与类名相同,且没有返回值。下面是一个简单的类示例: ```c++ class MyClass { public: // 构造函数 MyClass() { // 初始化对象 } // 析构函数 ~MyClass() { // 释放对象 } }; ``` 在上述代码中,MyClass是一个类,它包含了一个构造函数和一个析构函数。当程序实例化MyClass对象时,构造函数会被调用,用于初始化对象;当程序销毁MyClass对象时,析构函数会被调用,用于释放对象占用的内存空间和资源。 对于动态分配内存的对象,需要使用new和delete关键字来进行内存的分配和释放。下面是一个简单的示例代码: ```c++ MyClass* myObj = new MyClass(); // 动态分配MyClass对象的内存空间 delete myObj; // 释放MyClass对象的内存空间 ``` 在上述代码中,使用new关键字动态分配了一个MyClass对象的内存空间,并返回该对象的指针。在使用完该对象后,使用delete关键字释放该对象的内存空间。 需要注意的是,使用new动态分配内存空间后,需要手动释放该内存空间,否则会产生内存泄漏。同时,使用delete释放内存空间后,需要将指针置为NULL,以避免出现野指针。 总之,C++语言中的类构造函数析构函数是用于初始化和销毁对象的函数,而new和delete关键字则是用于动态分配和释放对象内存空间的关键字。程序员需要注意在使用new和delete时,要避免内存泄漏和野指针的产生。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值