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】的更多相关文章

  1. EC笔记:第4部分:21、必须返回对象时,别返回引用

    使用应用可以大幅减少构造函数与析构函数的调用次数,但是引用不可以滥用. 如下: struct St { int a; }; St &func(){ St t; return t; } 在返回t ...

  2. 【21】必须返回对象时,别妄想返回器reference

    1.考虑有理数Rational,有个友元操作符*,返回Rational对象.返回对象,导致临时对象的构造,析构.效率低,因此会想返回方法内局部对象的引用,这种方法不可行.为什么? 2.调用方法是在st ...

  3. 条款21: 必须返回对象时,不要强行返回对象的reference

    总结: 绝不要返回一个local栈对象的指针或引用:绝不要返回一个被分配的堆对象的引用:绝不要返回一个静态局部对象(为了它,有可能同时需要多个这样的对象的指针或引用). 条款4中给出了“在单线程环境中 ...

  4. [Effective C++ --021]必须返回对象时,别妄想返回其reference

    引言 在条目20中,我们知道了值传递和引用传递的效率问题,因此在设计程序时,我们可能就尽可能来返回引用而不是值. 可是,可能会犯下面的一些错误:传递一些引用指向其实并不存在的对象. 第一节:返回临时变 ...

  5. Effective C++ -----条款21:必须返回对象时,别妄想返回其reference

    绝不要返回pointer或reference指向一个local stack对象,或返回reference指向一个heap-allocated对象,或返回pointer或reference指向一个loc ...

  6. 条款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 ...

  7. 读书笔记_Effective_C++_条款二十一:当必须返回对象时,别妄想返回其reference

    在栈空间的临时成员变量在函数生命期结束后无法传出 friend A& operator*(const A& a, const A& b) { A temp; temp.data ...

  8. C++ 函数返回对象时并没有调用拷贝构造函数

    #include <iostream> #include <vector> #include <string.h> using namespace std; cla ...

  9. 读书笔记 effective c++ Item 21 当你必须返回一个对象的时候,不要尝试返回引用

    1. 问题的提出:要求函数返回对象时,可以返回引用么? 一旦程序员理解了按值传递有可能存在效率问题之后(Item 20),许多人都成了十字军战士,决心清除所有隐藏的按值传递所引起的开销.对纯净的按引用 ...

随机推荐

  1. 关于Swiper和vue数据顺序加载问题处理

    在使用swiper插件的时候,常常因为异步加载数据产生的顺序问题而使插件不能正常实行,所以可以使用vue的updated来解决. 问:什么时候 进updated方法? 答:只有事先设置好的data变量 ...

  2. input框处理大全

    1.去掉谷歌input记住账号或密码时默认出现的黄色背景: 直接用css的内阴影来覆盖黄色(代码中 white可换成其他颜色) input:-webkit-autofill { -webkit-box ...

  3. 十六, Oracle约束

    前言 数据的完整性用于确保数据库数据遵从一定的商业和逻辑规则,在oracle中,数据完整性可以使用约束.触发器.应用程序(过程.函数)三种方法来实现,在这三种方法中,因为约束易于维护,并且具有最好的性 ...

  4. 第 3 篇:实现博客首页文章列表 API

    作者:HelloGitHub-追梦人物 文中所涉及的示例代码,已同步更新到 HelloGitHub-Team 仓库 此前在讨论基于模板引擎的开发方式和 django-rest-framework 开发 ...

  5. 事件总线功能库,Reface.EventBus 详细使用教程

    Reface.AppStarter 中的事件总线功能是通过 Reface.EventBus 提供的. 参考文章 : Reface.AppStarter 框架初探 使用 Reface.EventBus ...

  6. Font-Awesome使用教程

    何为Font-Awesome Font Awesome gives you scalable vector icons that can instantly be customized — size, ...

  7. 瑞银预计小扎的十年规划可获大回报 上调Facebook股票目标价

    瑞银认为马克·扎克伯格(Mark Zuckerberg)为Facebook定制的十年规划将带来丰厚回报,它已将Facebook股票的目标价由之前的155美元上调至165美元. Facebook首席执行 ...

  8. html入门详细笔记

    Web的基本概念 什么是Web? 中文翻译"网页",它是一些列技术的总称,(包括网站的前台布局.后台程序.美工.数据库开发等),我们称它为网页. Web标准 结构标准(HTML) ...

  9. 关于SQL语言,这些你不得不了解!

    一.SQL概述 关系数据库标准语言SQL(结构化查询语言). 结构化查询语言(Structured Query Language)简称SQL,是一种特殊目的的编程语言,是一种数据库查询和程序设计语言, ...

  10. CF思维联系–CodeForces - 225C. Barcode(二路动态规划)

    ACM思维题训练集合 Desciption You've got an n × m pixel picture. Each pixel can be white or black. Your task ...