类中的构造函数,析构函数,复制构造函数,赋值函数

本文详细介绍了C++中构造函数、析构函数及赋值函数的基本概念与使用方法,并通过具体示例阐述了复制构造函数与赋值函数的区别及其应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

构造函数、析构函数、赋值函数是每个类最基本的的函数。每个类只有一个析构函数和一个赋值函数。但是有很多构造函数(一个为复制构造函数,其他为普通构造函数。对于一个类A,如果不编写上述四个函数,c++编译器将自动为A产生四个默认的函数,即:

  • A(void)                                    //默认无参数构造函数
  • A(const A &a)                         //默认复制构造函数
  • ~A(void);                                //默认的析构函数

  • A & operator = (const A &a); //默认的赋值函数
复制构造函数的使用:
              > 当用一个已经存在的对象去初始化一个新对象时
              > 当形参和实参都是对象,实参与形参进行结合时
              > 当函数的返回值是对象,return语句完成之时
赋值构造函数:对两个已经存在的函数使用
构造函数和复制构造函数的例子:
 ///
 /// @file    Computer1.cc
 /// @author  miaobeihai(452686191@mail.com)
 /// @date    2017-04-11 11:42:38
 ///

 
#include <string.h> // C的头文件放在C++头文件的前面

#include <iostream>
using std::cout;
using std::endl;

class Computer
{
public:
	Computer(const char * brand, float fPrice)//构造函数
	: _brand(new char[strlen(brand) + 1])
	, _fPrice(fPrice)
	{
		cout << "Computer(const char *, float)" << endl;
		strcpy(_brand, brand);
	}

	void print();

	//这是系统为我们提供的复制构造函数
#if 0
	Computer(const Computer & rhs)
	: _brand(rhs._brand) //浅拷贝
	, _fPrice(rhs._fPrice)
	{
		cout << "Computer(const Computer &)" << endl;
	}
#endif
	//复制构造函数的参数不能是对象,如果是,就会产生无穷递归
	//使用引用就可以打破无穷递归
	//const关键字不能去掉, 非const引用不能绑定到右值
	Computer(const Computer & rhs) //复制构造函数
	: _brand(new char[strlen(rhs._brand) + 1])//深拷贝
	, _fPrice(rhs._fPrice)
	{
		strcpy(_brand, rhs._brand);
		cout << "Computer(const Computer &)" << endl;
	}

	~Computer()
	{
		cout << "~Computer()" << endl;
		delete [] _brand;
	}

private:
	char  * _brand;
	float _fPrice;
};

void Computer::print()
{
	cout << "品牌:" << _brand << endl
		 << "价格:" << _fPrice << endl;
}


int main(void)
{
	Computer com("Mac", 10000);
	com.print();
	//com.~Computer();//析构函数可以显式调用,但一般不这样用
	
	Computer com2 = com;
	com2.print();

	const Computer & com3 = Computer("IBM", 7000);
	
#if 1
	int a = 10;
	int & ref = a;
	const int & ref1 = 10;//10叫右值, 不能进行取地址
#endif

	return 0;
}


运行结果如下:
Computer(const char *, float)
品牌:Mac
价格:10000
Computer(const Computer &)
品牌:Mac
价格:10000
Computer(const char *, float)
~Computer()
~Computer()
~Computer()

构造函数和赋值构造函数:
 ///
 /// @file    Computer1.cc
 /// @author  miaobeihai(452686191@mail.com)
 /// @date    2017-04-11 11:42:38
 ///

 
#include <string.h> // C的头文件放在C++头文件的前面

#include <iostream>
using std::cout;
using std::endl;

class Computer
{
public:
	Computer(const char * brand, float fPrice)
	: _brand(new char[strlen(brand) + 1])
	, _fPrice(fPrice)
	{
		cout << "Computer(const char *, float)" << endl;
		strcpy(_brand, brand);
	}

	void print();

	//这是系统为我们提供的复制构造函数
#if 0
	Computer(const Computer & rhs)
	: _brand(rhs._brand) //浅拷贝
	, _fPrice(rhs._fPrice)
	{
		cout << "Computer(const Computer &)" << endl;
	}
#endif
	//复制构造函数的参数不能是对象,如果是,就会产生无穷递归
	//使用引用就可以打破无穷递归
	//const关键字不能去掉, 非const引用不能绑定到右值
	Computer(const Computer & rhs)
	: _brand(new char[strlen(rhs._brand) + 1])//深拷贝
	, _fPrice(rhs._fPrice)
	{
		strcpy(_brand, rhs._brand);
		cout << "Computer(const Computer &)" << endl;
	}


#if 1
	Computer & operator=(const Computer & rhs)
	{
		cout << "Computer & operator=(const Computer&) " << endl;

		if(this == &rhs)
			return *this;// 先考虑自复制的问题

		delete [] _brand;//再回收原来的开辟的空间

		_brand = new char[strlen(rhs._brand) + 1];//最后进行深拷贝
		strcpy(_brand, rhs._brand);

		this->_fPrice = rhs._fPrice;
		return *this;
	}
#endif

	~Computer()
	{
		cout << "~Computer()" << endl;
		delete [] _brand;
	}

private:
	char  * _brand;
	float _fPrice;
};

void Computer::print()
{
	cout << "品牌:" << _brand << endl
		 << "价格:" << _fPrice << endl;
}


int main(void)
{
	Computer com("Mac", 10000);
	com.print();
	
	Computer com2("IBM", 7000);
	com2.print();

	cout << "进行赋值之后:" << endl;
	com2 = com;
	com2.print();

	//com = com;
	

	return 0;
}


运行结果如下:
Computer(const char *, float)
品牌:Mac
价格:10000
Computer(const char *, float)
品牌:IBM
价格:7000
进行赋值之后:
Computer & operator=(const Computer&) 
品牌:Mac
价格:10000
~Computer()
~Computer()


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值