必须返回对象时,别妄想返回其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),许多人都成了十字军战士,决心清除所有隐藏的按值传递所引起的开销.对纯净的按引用 ...
随机推荐
- CRS-0184 Cannot communicate with the CRS daemon
事件背景 rman清理脚本异常.导致磁盘空间爆满(一个环境变量没有设置正确) 释放磁盘空间,进行rman清理 之后,领导把实例重启,但是ams实例没有关闭 环境 系统 : AIX 数据库: Oracl ...
- AjaxControlToolkit的安装步骤
1.下载: 下载地址:http://www.codeplex.com/AtlasControlToolkit/Release/ProjectReleases.aspx 打开网址后找到这些: AjaxC ...
- Java ASM3学习(3)
MethodVisitor ClassVisitor的visitMethod能够访问到类中某个方法的一些入口信息,那么针对具体方法中字节码的访问是由MethodVisitor来进行的 访问顺序如下,其 ...
- LaTex中文article模板(支持代码、数学、TikZ)
代码 请使用XeLatex编译 main.tex \documentclass{article} \usepackage{ctex} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ...
- Vue Cli 报错:You are using the runtime-only build of Vue where the template compiler is not availabl
报错原因: 这里引用的是vue.runtime.esm.js,造成的不能正常运行. vue-cli 2.x 解决方法: 在webpack.base.conf.js配置文件中多加了一段代码,将 vue/ ...
- CentOS+Subversion 配置Linux 下 SVN服务器
1.安装#yum install subversion测试安装是否成功:#svnserve –version 回车显示版本说明安装成功 2.配置 ·建立版本库 #mkdir /opt/svnd ...
- .NET Core+WebApi+EF访问数据新增用户数据
新建一个.NET Core项目,我使用的IDE是VS2019 依次创建三个Core类库:第一个命名api.Model,第二个api.Common,第三个api.Bo 解释一下这个三类库的作用: 第一个 ...
- Linux下文件完整性监控工具Tripwire详解
Tripwire 是目前最为著名的Unix下文件系统完整性检查的软件工具,这一软件采用的技术核心就是对每个要监控的文件产生一个数字签名,保留下来.当文件现在的数字签名与保留的数字签名不一致时,那么现在 ...
- CF思维联系–CodeForces - 225C. Barcode(二路动态规划)
ACM思维题训练集合 Desciption You've got an n × m pixel picture. Each pixel can be white or black. Your task ...
- 多线程高并发编程(7) -- Future源码分析
一.概念 A Future计算的结果. 提供方法来检查计算是否完成,等待其完成,并检索计算结果. 结果只能在计算完成后使用方法get进行检索,如有必要,阻塞,直到准备就绪. 取消由cancel方法执行 ...