【C/C++】返回值与左值

关于左值右值的定义,感觉好烦的样子,这里我们仅仅探讨返回值与左值的关系。

参考自:函数返回值作为左值问题(sunshinewave)


左值,简单来说就是可以放在等号左边被赋值。

为了更好讨论这个问题,我们首先将函数返回值分为C++内置类型与自定义类型。


对于内置类型,当返回值为值传递时,返回值实际是临时变量,不能作为左值,要想做左值,只能是返回引用。如下:

// 值传递
#include <iostream>

int GetInt() { return 3; }

int main () {
	GetInt() = 4;
	return 0;
}

编译报错:

a.cc: In function ‘int main()’:
a.cc:6:11: error: lvalue required as left operand of assignment
  GetInt() = 4;
           ^


// 引用传递
#include <iostream>

int& GetInt() { 
	int* p = new int;
	return *p; 
}

int main () {
	GetInt() = 4;
	return 0;
}

这次就没问题了,只不过内存泄露了o(╯□╰)o

// 常引用传递
#include <iostream>

const int& GetInt() { 
	int* p = new int;
	return *p; 
}

int main () {
	GetInt() = 4;
	return 0;
}
编译报错,返回值为常引用,不能被修改。

考虑到当值传递时返回值实际是个临时变量,我们就别用 const 修饰了,没用的。


自定义类型与之类似:值传递的结果不能作为左值,但是可以调用成员函数。我们看个例子:

#include <iostream>

class Student {
	public:
		Student(const std::string& name, const int age):name_(name),age_(age) {
			std::cout << "constructor" << std::endl;
		}
		Student(const Student& stu) {
			std::cout << "copy-constructor" << std::endl;
			if (this != &stu) {
				name_ = stu.name_;
				age_ = stu.age_;
			}
		}
		Student& operator= (const Student& stu) {
			std::cout << "operator-constructor" << std::endl;
			if (this != &stu) {
				name_ = stu.name_;
				age_ = stu.age_;
			}
		}
		void Display() { std::cout << name_ << ": " << age_ << std::endl; }
		~Student() { std::cout << "destrutor" << std::endl; }
	private:
		std::string name_;
		int age_;
};

Student Init(const std::string& name, const int age) {
	return Student(name, age);
}


int main () {
	Student stu1("lb", 26);
	Init("zyj", 26) = stu1;
	std::cout << "--------------------" << std::endl;
	return 0;
}


运行结果如下:

constructor
constructor
operator-constructor
destrutor
--------------------
destrutor

这里需要注意的是Init()的返回值确实不是左值,但为什么能够正确执行等号?答案是:对于自定义类型,我们重载了等号,所以实际上是调用了成员函数,这是允许的。只不过返回值作为一个临时变量

Init("zyj", 26) = stu1;
这句话执行完毕就析构了。细心的人可能会发现貌似少了一次拷贝构造与析构,原因是RVO,我们使用 -fno-elide-constructors 可以不做RVO优化,得到如下结果:

constructor
constructor
copy-constructor
destrutor
operator-constructor
destrutor
--------------------
destrutor


同样的,返回如果是引用,就是左值,调用成员函数当然没有问题的。

#include <iostream>

class Student {
	public:
		Student(const std::string& name, const int age):name_(name),age_(age) {
			std::cout << "constructor" << std::endl;
		}
		Student(const Student& stu) {
			std::cout << "copy-constructor" << std::endl;
			if (this != &stu) {
				name_ = stu.name_;
				age_ = stu.age_;
			}
		}
		Student& operator= (const Student& stu) {
			std::cout << "operator-constructor" << std::endl;
			if (this != &stu) {
				name_ = stu.name_;
				age_ = stu.age_;
			}
		}
		void Display() { std::cout << name_ << ": " << age_ << std::endl; }
		~Student() { std::cout << "destrutor" << std::endl; }
	private:
		std::string name_;
		int age_;
};

Student& Init(const std::string& name, const int age) {
	return *(new Student(name, age));
}


int main () {
	Student stu1("lb", 26);
	Init("zyj", 26) = stu1;
	std::cout << "--------------------" << std::endl;
	return 0;
}


输出结果:

constructor
constructor
operator-constructor
--------------------
destrutor

但如果返回类型加入 const 关键字,调用成员函数没问题,但只能调用 const 修饰的成员函数,否则编译会报错,如下:

#include <iostream>

class Student {
	public:
		Student(const std::string& name, const int age):name_(name),age_(age) {
			std::cout << "constructor" << std::endl;
		}
		Student(const Student& stu) {
			std::cout << "copy-constructor" << std::endl;
			if (this != &stu) {
				name_ = stu.name_;
				age_ = stu.age_;
			}
		}
		Student& operator= (const Student& stu) {
			std::cout << "operator-constructor" << std::endl;
			if (this != &stu) {
				name_ = stu.name_;
				age_ = stu.age_;
			}
		}
		void Display() { std::cout << name_ << ": " << age_ << std::endl; }
		~Student() { std::cout << "destrutor" << std::endl; }
	private:
		std::string name_;
		int age_;
};

const Student& Init(const std::string& name, const int age) {
	return *(new Student(name, age));
}


int main () {
	Student stu1("lb", 26);
	Init("zyj", 26) = stu1;
	std::cout << "--------------------" << std::endl;
	return 0;
}

编译报错:

a.cc: In function ‘int main()’:
a.cc:36:18: error: passing ‘const Student’ as ‘this’ argument discards qualifiers [-fpermissive]
  Init("zyj", 26) = stu1;
                  ^
a.cc:15:12: note:   in call to ‘Student& Student::operator=(const Student&)’
   Student& operator= (const Student& stu) {

由于 operator= 重载并未被 const 修饰,所以无法被 const 修饰的对象调用,不论是值还是引用。

  • 5
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值