C/C++返回值与左值

Posted mick_seu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 修饰的对象调用,不论是值还是引用。

以上是关于C/C++返回值与左值的主要内容,如果未能解决你的问题,请参考以下文章

C语言 啥叫做左值?右值?

左值和右值的区别

C++内功修炼干货,进大厂必须会的C++左值与右值,最适合小白看的文章!

三句话回答C/C++面试题

C++ 专题 右值引用移动语义与完美转发

左值与右值引用 详解