27 C++ 临时对象

学习临时对象的目的

了解临时对象产生的原因,改进代码让尽量产生临时产量,因为临时变量会消耗系统资源。

产生临时对象的3中情况以及解决方案。

第一种:以值的方式给函数传递参数

class Teacher75 {
public:
	Teacher75() {
		cout << "Teacher75 构造函数" << this<< endl;
	}
	Teacher75(const Teacher75 & t) {
		this->m_age = t.m_age;
		cout << "Teacher75 copy 构造函数" << this<< "   t = " << &t<<endl;
	}

	Teacher75& operator=(const Teacher75 & t) {
		cout << "Teacher75 operator =  函数" << this<<endl;
		this->m_age = t.m_age;
		return *this;
	}
	int m_age;
};

//第一种:以值的方式给函数传递参数,这里参数是 Teacher75,
//当实参传递给形参的时候,会调用copy 构造函数,将实参传递copy 给形参 
void getTeacher75age(Teacher75 t) {
	cout << t.m_age << endl;
}

//第一种解决方案:
void getTeacher75agefix(Teacher75& t) {
	cout << t.m_age << endl;
}
void main() {
	Teacher75 t1;
	t1.m_age = 28;
	//第一种:以值的方式给函数传递参数
	getTeacher75age(t1);

	cout << "-----" << endl;
	Teacher75 t2;
	t2 = t1;
	cout << "---使用引用的方式传递函数参数---" << endl;
	getTeacher75agefix(t1);
}

第二种,函数返回临时对象的时候

//第二种,函数返回临时对象的时候
class Teacher76 {
public:
	Teacher76() {
		cout << "Teacher76 构造函数" << this << endl;
	}
	Teacher76(int age):m_age(age) {
		cout << "Teacher76 构造函数" << this << endl;
	}
	Teacher76(const Teacher76 & t) {
		this->m_age = t.m_age;
		cout << "Teacher76 copy 构造函数" << this << "   t = " << &t << endl;
	}

	Teacher76& operator=(const Teacher76 & t) {
		cout << "Teacher76 operator =  函数" << this << endl;
		this->m_age = t.m_age;
		return *this;
	}
	~Teacher76() {
		cout << "Teacher76 析构函数" << this << endl;
	}
	int m_age;

};

//问题,实际上是多一次 copy 构造函数调用
Teacher76 getTeacher76(){
	Teacher76 temp; //Teacher76 构造函数000000A584D8F564
	temp.m_age = 87;
	return temp; //Teacher76 copy 构造函数000000A584D8F784   t = 000000A584D8F564
}

//解决方案,在可以的case下 直接return 
Teacher76 getTeacher76fix() {
	return Teacher76(87); 
}

void main() {
	getTeacher76();

	//  Teacher76 构造函数000000A584D8F564  temp 构造函数被调用
	//	Teacher76 copy 构造函数000000A584D8F784   t = 000000A584D8F564   return 时,会调用copy 构造函数
	//  多了一个构造函数
	//	Teacher76 析构函数000000A584D8F564  // temp 被析构
	//	Teacher76 析构函数000000A584D8F784 //返回的copy出来的这个构造函数,没有接,因此也析构

	cout << "断点在这里" << endl;

	getTeacher76fix();
	//  Teacher76 构造函数00000085234FFB94
	//	Teacher76 析构函数00000085234FFB94
}

第三种 隐式类型转换

//第三种 隐式类型转换
int count(const string& source,char ch) {
	return 8;
}
void main() {
	char charshuzu[100] = "abcc";
	int ncount = count(charshuzu,'a');
	//这里调用为了能调用成功, charshuzu会被从char [100],隐式转换成 const string 。

	//这会有隐式转换发生。

	//改法:不要让C++编译器帮忙转,自己转。
	string mystr = "mnv";
	int ncount222 = count(mystr, 'a');
}

相关推荐

  1. 27 C++ 临时对象

    2024-01-05 13:42:03       42 阅读
  2. C++临时对象的产生及优化

    2024-01-05 13:42:03       9 阅读
  3. c#生成临时文件

    2024-01-05 13:42:03       32 阅读
  4. 22.Oracle中的临时表空间

    2024-01-05 13:42:03       44 阅读
  5. g 对象:Flask 应用中的“临时口袋”

    2024-01-05 13:42:03       13 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-05 13:42:03       19 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-05 13:42:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-05 13:42:03       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-05 13:42:03       20 阅读

热门阅读

  1. 基于 Makefile 的 FPGA 构建系统

    2024-01-05 13:42:03       29 阅读
  2. 最新ChatGPT分享(2024-1月)

    2024-01-05 13:42:03       37 阅读
  3. element ui backTop源码解析-逐行逐析

    2024-01-05 13:42:03       38 阅读
  4. sql索引详解

    2024-01-05 13:42:03       38 阅读
  5. Do you know about domestic CPUs

    2024-01-05 13:42:03       43 阅读
  6. MySQL 中的状态变量

    2024-01-05 13:42:03       35 阅读
  7. AI:114-基于深度学习的卫星图像目标识别

    2024-01-05 13:42:03       43 阅读
  8. 人机之间的联系

    2024-01-05 13:42:03       44 阅读
  9. delphi中自定义自己的定时器

    2024-01-05 13:42:03       45 阅读
  10. Golang如何解决重复提交并发问题

    2024-01-05 13:42:03       44 阅读