【C/C++】返回值与左值
来源:互联网 发布:ipa手游模拟器mac 编辑:程序博客网 时间:2024/05/21 06:59
关于左值右值的定义,感觉好烦的样子,这里我们仅仅探讨返回值与左值的关系。
参考自:函数返回值作为左值问题(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;}
运行结果如下:
constructorconstructoroperator-constructordestrutor--------------------destrutor
这里需要注意的是Init()的返回值确实不是左值,但为什么能够正确执行等号?答案是:对于自定义类型,我们重载了等号,所以实际上是调用了成员函数,这是允许的。只不过返回值作为一个临时变量
Init("zyj", 26) = stu1;这句话执行完毕就析构了。细心的人可能会发现貌似少了一次拷贝构造与析构,原因是RVO,我们使用 -fno-elide-constructors 可以不做RVO优化,得到如下结果:
constructorconstructorcopy-constructordestrutoroperator-constructordestrutor--------------------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;}
constructorconstructoroperator-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 修饰的对象调用,不论是值还是引用。
阅读全文
0 0
- 【C/C++】返回值与左值
- C与C++左值
- C 左值与右值
- 【C语言】左值与右值
- Modern C++(四)左值与右值
- C语言函数说明与返回值
- C语言之函数与返回值
- 引用返回左值
- C语言左值与右值的区别(C语言深度剖析读书笔记一)
- C:关于返回值
- c/c++ 左值 右值讨论
- C语言左值和右值
- C/C++左值&右值
- 左值表达式 右值表达式(C/C++)
- 引用左值(返回引用)
- C和指针(二)指针与左值、右值详解
- 自增自减操作符在C与C++中的左值区别
- C语言与C++函数的返回值
- 2.服务注册中心(Eureka服务注册与发现)
- 再次看看 malloc free
- 航空公司客户价值分析
- Apple Catching
- oc入门
- 【C/C++】返回值与左值
- 基于R语言构建的电影评分预测模型
- 二哥的OJ 1251-final
- 曾经误入传销我和linux系统结下缘分
- 学习Spring Cloud第十一课(Feign的简介及基础使用)
- 菜鸟学习历程【15-4】冒泡排序
- php swoole扩展编译
- Unity UGUI基础之Image
- C语言的常量