栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++赋值函数,为什么通常需要返回类的引用

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++赋值函数,为什么通常需要返回类的引用

C++赋值函数返回类的引用

C++赋值函数,为什么通常需要返回类的引用,其实主要目的是优化性能。在调用例如 a = b 或者 a = b = c的时候,减少复制构造函数的调用,而复制构造函数通常在函数中,按值类型传递参数或者更返回时会调用到,这篇文章主要解释赋值函数;例如下面的示例代码:

#include
#include


using namespace std;

class A
{
public:
	char* str;

	A& operator= (const A& b);  //赋值构造函数, 这里时返回引用类型

	A(const A& b);

	A()
	{
		str = new char[4];
		strcpy_s(str, 4, "c++");
	}

	A(const char *s)
	{
		str = new char[strlen(s) + 1];
		strcpy_s(str, strlen(s) + 1, s);
	}
};

A& A::operator=(const A& b)    //赋值构造函数, 这里时返回引用类型
{
	cout << "赋值函数调用" << endl;
	if (this == &b) return *this;
	delete [] str;
	str = new char[strlen(b.str) + 1];
	strcpy_s(str, strlen(b.str) + 1, b.str);
	return *this;
}
A::A(const A& b)
{
	cout << "复制函数调用" << endl;
	int len = strlen(b.str);
	str = new char[len + 1];
	strcpy_s(str, len + 1, b.str);
}

int main()
{
	const char* s = " 996 ~";
	A a(s);
	A b;
	A c;
	cout << string(100, '-') << endl;
	c = b = a;
	//c.operator=(b.operator=(a)); // 和c = b = a 一样
	cout << string(100, '-') << endl;
	return 0;
}

这里输出的结果如下:

这是在赋值函数返回的时类的引用的情况下,执行c = b = a只调用了两次类的赋值函数。
当我们把赋值函数的返回改为普通值类型,修改之后的代码如下:

#include
#include


using namespace std;

class A
{
public:
	char* str;

	A operator= (const A& b);  //赋值构造函数, 这里时返回值类型

	A(const A& b);

	A()
	{
		str = new char[4];
		strcpy_s(str, 4, "c++");
	}

	A(const char *s)
	{
		str = new char[strlen(s) + 1];
		strcpy_s(str, strlen(s) + 1, s);
	}
};

A A::operator=(const A& b)  //赋值构造函数, 这里时返回值类型
{
	cout << "赋值函数调用" << endl;
	if (this == &b) return *this;
	delete [] str;
	str = new char[strlen(b.str) + 1];
	strcpy_s(str, strlen(b.str) + 1, b.str);
	return *this;
}
A::A(const A& b)
{
	cout << "复制函数调用" << endl;
	int len = strlen(b.str);
	str = new char[len + 1];
	strcpy_s(str, len + 1, b.str);
}

int main()
{
	const char* s = " 996 ~";
	A a(s);
	A b;
	A c;
	cout << string(100, '-') << endl;
	c = b = a;
	//c.operator=(b.operator=(a)); // 和c = b = a 一样
	cout << string(100, '-') << endl;
	return 0;
}

此时的输出结果如下:

与上一个相比,多调用了两次复制函数,当我们在使用b = c时,实际上等价与调用 b.operator=(a),按照C++ Primmer介绍的,无论哪种编译器,当按值传递和返回对象时,都会调用复制构造函数,所以,我们可以看到,在大部分介绍C++的书籍中,在赋值函数和复制构造函数中,传参数的类型都是引用类型,赋值函数返回的类型也是引用类型,其目的就是为了减少复制构造函数的调用,复制构造函数中,有可能会涉及到内存的分配,所以会造成一定程度的性能消耗。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1033552.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号