【C++】类和对象2

赋值运算符的重载函数(若系统默认给出的赋值运算符的重载函数为浅拷贝函数)

实现过程:1.判断是否为自赋值   2.释放旧资源     3.申请新资源    4.赋值

const:1.防止修改实参   2.接收隐式生成的临时对象

#include <iostream>

class Test
{
public:
	Test(int a, int b)
	{
		std::cout << this << " :Test::Test(int,int)" << std::endl;
		ma = a;
		mb = b;
	}
	Test(int a)
	{
		std::cout << this << " :Test::Test(int)" << std::endl;
		ma = a;
		mb = 0;
	}
	Test()
	{
		std::cout << this << " :Test::Test()" << std::endl;
		ma = mb = 0;
	}
	Test(const Test& rhs)
	{
		std::cout << this << " :Test::Test(const Test&)" << std::endl;
		ma = rhs.ma;
		mb = rhs.mb;
	}
	Test& operator=(const Test& rhs)
	{
		std::cout << this << " :Test::operator=(const Test&)" << std::endl;
		if (this != &rhs)
		{
			ma = rhs.ma;
			mb = rhs.mb;
		}
		return *this;
	}
	~Test()
	{
		std::cout << this << " :Test::~Test()" << std::endl;
	}
	int getValue()
	{
		return ma;
	}
private:
	int ma;
	int mb;
};
int main()
{
	Test test1(10, 20);
	Test test2;
	return 0;
}

 

临时量:

       1.内置类型 --> 常量

       2.自定义类型 --> 变量

       3.隐式生存的临时对象 --> 常量

临时对象的生存周期:

         表达式结束;

    优化:

      临时对象生成的目的,为了生成新的对象。以生成临时对象的方式生成新对象。

      引用能提升临时对象的生存周期,把临时对象提升和引用变量相同的生存周期

#include <iostream>


class CGoods
{
public:
	CGoods(char* name, float price, int amount)
	{
		std::cout << this << " :CGoods::CGoods(char*,float, int)" << std::endl;
		mname = new char[strlen(name) + 1]();
		strcpy(mname, name);
		mprice = price;
		mamount = amount;
	}
	CGoods(int amount)
	{
		std::cout << this << " :CGoods::CGoods(int)" << std::endl;
		mname = new char[1]();
		mamount = amount;
	}
	CGoods()
	{
		std::cout << this << " :CGoods::CGoods()" << std::endl;
		mname = new char[1]();
	}
	~CGoods()
	{
		std::cout << this << " :CGoods::~CGoods()" << std::endl;
		delete[] mname;
		mname = NULL;
	}
	CGoods(const CGoods& rhs)
	{
		std::cout << this << " :CGoods::CGoods(const CGoods&)" << std::endl;
		mname = new char[strlen(rhs.mname) + 1]();
		strcpy(mname, rhs.mname);
		mprice = rhs.mprice;
		mamount = rhs.mamount;
	}
	CGoods& operator=(const CGoods& rhs)
	{
		std::cout << this << " :CGoods::operator=(const CGoods&)" << std::endl;
		if (this != &rhs)
		{
			delete[] mname;
			mname = new char[strlen(rhs.mname) + 1]();
			strcpy(mname, rhs.mname);
			mprice = rhs.mprice;
			mamount = rhs.mamount;
		}
		return *this;
	}
private:
	char* mname;
	float mprice;
	int mamount;
};

CGoods ggood1("good1", 10.1, 20);//调用构造函数,在.data,生存周期在程序开始到程序结束
int main()
{
	CGoods good3;//调用默认构造函数,生存周期为调用点开始到main函数结束
	CGoods good4(good3);//拷贝构造函数,生存周期为调用点开始到main函数结束

	good4 = good3;//赋值运算符的重载函数,生存周期为调用点开始到main函数结束

	static CGoods good5("good5", 10.1, 20);//构造函数,在.data,生存周期为调用点开始到程序运行结束

	CGoods good6 = 10;//隐式临时对象(优化)生存周期为表达式结束,调用构造函数
	CGoods good7(10);//显示临时对象(优化)生存周期为表达式结束,调用构造函数
	CGoods good8 = CGoods("good8", 10.1, 20);//显示临时对象(优化)生存周期为表达式结束,调用构造函数

	good6 = 20;//隐式临时对象生存周期为表达式结束,调用构造函数、赋值运算符重载函数、析构函数
	good7 = CGoods(20);//显式临时对象生存周期为表达式结束,调用构造函数、赋值运算符重载函数、析构函数
	good8 = (CGoods)("good8",10.1, 20);//逗号表达式,显式临时对象生存周期为表达式结束,调用构造函数、赋值运算符重载函数、析构函数

	CGoods* pgood9 = new CGoods("good9", 10.1, 20);//构造函数,在heap,生存周期为new开始到delete结束
	CGoods* pgood10 = new CGoods[2];//调用构造函数,在heap,生存周期为new开始到delete结束

	CGoods* pgood11 = &CGoods("good11", 10.1, 20);//显式临时对象生存周期为表达式结束,将临时对象的地址传递给pgood11但是表达式一结束地址就会消失,调用构造函数、赋值运算符重载函数、析构函数
	std::cout << "------------------" << std::endl;
	//CGoods* pgood12 = 20;//一个指针是内置类型
	CGoods& rgood12 = CGoods("good11", 10.1, 20);//显式临时对象生存周期为rgood12的生存周期(引用提高了临时对象的生存周期),将临时对象的地址传递给rgood12,调用构造函数、赋值运算符重载函数、析构函数
	std::cout << "------------------" << std::endl;
	const CGoods& rgood13 = 20;//隐式临时对象生存周期为rgood13的生存周期(引用提高了临时对象的生存周期),将临时对象的地址传递给rgood12,调用构造函数、赋值运算符重载函数、析构函数,内置类型生成的临时量是常量

	delete pgood9;
	delete[] pgood10;

	return 0;
}
CGoods ggood2("good2", 10.1, 20);//调用构造函数,在.data,生存周期在程序开始到程序结束

explicit
        禁止隐式生成临时对象

volatile

        禁止编译器优化

mutable

        

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值