必须返回对象时,别妄想返回其reference 【Effective C++ 条款21】
class Rational
{
public:
Rational(int numerator = , int denominator = ) : n(numerator), d(denominator) {
printf("Rational Constructor\n");
}
~Rational() {
printf("Rational Destructor\n");
}
Rational(const Rational& rhs) {
this->d = rhs.d;
this->n = rhs.n;
printf("Rational Copy Constructor\n");
}
private:
int n, d;
friend const Rational operator*(const Rational& lhs, const Rational& rhs);
};
Rational的*运算符可以这样重载:
const Rational operator*(const Rational& lhs, const Rational& rhs)
{
Rational tmp(lhs.n * rhs.n, lhs.d * rhs.d);
return tmp;
}
但是不可以这样重载:【区别在于一个&】
const Rational& operator*(const Rational& lhs, const Rational& rhs)
{
Rational tmp(lhs.n * rhs.n, lhs.d * rhs.d);
return tmp;
}
当这样去使用:
Rational x(, ), y(, );
Rational z = x * y;
第一种方法可以得到正确的结果,因为会调用Rational的拷贝构造函数将tmp赋给z,但是第二种方法返回的是tmp的引用,在函数退出前,tmp就被销毁了,所以这样做是不对的。
不过,第一种方法虽然功能上没有问题,但是效率上有所欠缺,因为调用了三次构造函数,一次复制构造函数,一次析构函数
Rational Constructor
Rational Constructor
Rational Constructor
Rational Copy Constructor
Rational Destructor
可以进行返回值优化如下:
const Rational operator*(const Rational& lhs, const Rational& rhs)
{
return Rational(lhs.n * rhs.n, lhs.d * rhs.d);
}
Rational Constructor
Rational Constructor
Rational Constructor
优化之后少调用了一次复制构造函数和析构函数
完整代码如下:
#include <stdio.h>
class Rational
{
public:
Rational(int numerator = , int denominator = ) : n(numerator), d(denominator) {
printf("Rational Constructor\n");
}
~Rational() {
printf("Rational Destructor\n");
}
Rational(const Rational& rhs) {
this->d = rhs.d;
this->n = rhs.n;
printf("Rational Copy Constructor\n");
}
private:
int n, d;
friend const Rational operator*(const Rational& lhs, const Rational& rhs);
}; const Rational operator*(const Rational& lhs, const Rational& rhs)
{
return Rational(lhs.n * rhs.n, lhs.d * rhs.d);
} int main()
{
Rational x(, ), y(, );
Rational z = x * y;
return ;
}
必须返回对象时,别妄想返回其reference 【Effective C++ 条款21】的更多相关文章
- EC笔记:第4部分:21、必须返回对象时,别返回引用
使用应用可以大幅减少构造函数与析构函数的调用次数,但是引用不可以滥用. 如下: struct St { int a; }; St &func(){ St t; return t; } 在返回t ...
- 【21】必须返回对象时,别妄想返回器reference
1.考虑有理数Rational,有个友元操作符*,返回Rational对象.返回对象,导致临时对象的构造,析构.效率低,因此会想返回方法内局部对象的引用,这种方法不可行.为什么? 2.调用方法是在st ...
- 条款21: 必须返回对象时,不要强行返回对象的reference
总结: 绝不要返回一个local栈对象的指针或引用:绝不要返回一个被分配的堆对象的引用:绝不要返回一个静态局部对象(为了它,有可能同时需要多个这样的对象的指针或引用). 条款4中给出了“在单线程环境中 ...
- [Effective C++ --021]必须返回对象时,别妄想返回其reference
引言 在条目20中,我们知道了值传递和引用传递的效率问题,因此在设计程序时,我们可能就尽可能来返回引用而不是值. 可是,可能会犯下面的一些错误:传递一些引用指向其实并不存在的对象. 第一节:返回临时变 ...
- Effective C++ -----条款21:必须返回对象时,别妄想返回其reference
绝不要返回pointer或reference指向一个local stack对象,或返回reference指向一个heap-allocated对象,或返回pointer或reference指向一个loc ...
- 条款21:必须返回对象时,别妄想返回其reference(Don't try to return a reference when you must return an object)
NOTE: 1.绝不要返回pointer或reference 指向一个local stack 对象,或返回reference 指向一个heap-allocated对象,或返回pointer 或refe ...
- 读书笔记_Effective_C++_条款二十一:当必须返回对象时,别妄想返回其reference
在栈空间的临时成员变量在函数生命期结束后无法传出 friend A& operator*(const A& a, const A& b) { A temp; temp.data ...
- C++ 函数返回对象时并没有调用拷贝构造函数
#include <iostream> #include <vector> #include <string.h> using namespace std; cla ...
- 读书笔记 effective c++ Item 21 当你必须返回一个对象的时候,不要尝试返回引用
1. 问题的提出:要求函数返回对象时,可以返回引用么? 一旦程序员理解了按值传递有可能存在效率问题之后(Item 20),许多人都成了十字军战士,决心清除所有隐藏的按值传递所引起的开销.对纯净的按引用 ...
随机推荐
- 2019-2020-1 20199325《Linux内核原理与分析》第二周作业
冯诺依曼计算机硬件框图: 下面是一个简单的程序example.c. intadd_a_and_b(int a,int b){returna+b;}intmain(){returnadd_a_and_b ...
- notepad正则删除关键词所在行
转自:http://www.gangzi.net/article/615.htm 查找:^.*大师兄.*$替换为:(空) 如果不留空行:查找:^.*大师兄.*\r?\n替换为:(空)注意:Notepa ...
- CSS躬行记(8)——裁剪和遮罩
一. 裁剪 裁剪(clipping)能让元素显示指定形状的区域,在布局时可起点缀的作用,丰富了视觉呈现.注意,裁剪本质上只是让元素的部分区域透明,由此可知,裁剪完后元素所占的空间仍旧会保留.裁剪最早是 ...
- 【java基础】01 计算机基础知识
一.计算机基础知识 1. 计算机 1. 什么是计算机? 计算机在生活中的应用举例 计算机(Computer)全称:电子计算机,俗称电脑.是一种能够按照程序运行,自动.高速处理海量数据的现代化智能电子设 ...
- js高阶函数的理解
高阶函数:英文叫Higher-order function.JavaScript的函数其实都指向某个变量.既然变量可以指向函数,函数的参数能接收变量,那么一个函数就可以接收另一个函数作为参数,这种函数 ...
- OpenRASP管理后台安装记录
OpenRASP项目地址https://rasp.baidu.com/ 一.安装java 在CentOS中安装ElasticSearch需要Java1.8.0,可执行命令java -version查看 ...
- RobotFrameWork 自动化环境搭建(基于 python2.7)
1.自动化工具安装顺序 robot Framework(两个RF框架) WXpython(不要更改安装路径,自动安装在python文件中) 安装依赖库 RF3.0 和 RF1.5.2.1 打开 rid ...
- Java——Java是什么一门什么语言
解释型语言 源代码不能直接翻译成机器语言,而是先翻译成中间代码,再由解释器对中间代码进行解释运行: 程序不需要编译,程序在运行时才翻译成机器语言,每执行一次都要翻译一次: 解释性语言代表:Python ...
- 模块_os模块
import os print(os.getcwd()) # 获取当前工作目录 print(os.listdir()) # 列表列出当前目录下的目录名和文件名 os.mkdir("tempd ...
- PI/PO Token配置
接收方通道配置 因为本例中需要在访问业务接口时,获取某平台的TOKEN认证,并在调用业务接口时,将TOKEN一同传给某平台,但是不能放在请求参数中,而是放在HTTP的Headers 注意!是Heade ...