C++中大致有三种返回值:值拷贝(副本),值引用和指针,返回什么类型的值要根据当时情况而定。

如果返回的是大型对象的副本,那么在每一次的函数调用后返回,都会调用该对象类型的拷贝构造函数构造一个新的副本,这是一个耗时的过程。

因此在不需要返回对象副本的时候,尽量返回对象的引用或者指针,与此同时,如果不会改变引用或者指针指向的对象的内容,都加上const限定符。

不论是返回拷贝,引用还是指针,返回值都会作为左值来使用,因此也可以将返回值赋值给其他变量(类型可以是值类型,引用类型或者指针类型)

实例:

class Widget
{
public:
Widget()
{
a = 0;
printf("Widget constructor with default value: this:%p a:%d \n", this, a);
}
Widget(int v)
{
a = v;
printf("Widget constructor with parameters: this:%p a:%d \n", this, a);
}
Widget(const Widget& widget)
{
this->a = widget.a;
printf("Widget copy constructor with reference: this:%p widget:%p a:%d \n", this, &widget, a);
}
Widget(const Widget* widget)
{
this->a = widget->a;
printf("Widget copy constructor with pointer: this:%p widget:%p a:%d \n", this, widget, a);
}
Widget& operator = (const Widget& widget)
{
this->a = widget.a;
printf("Widget operator =: this:%p widget:%p a:%d \n", this, &widget, a); return *this;
}
Widget& operator + (const Widget& widget)
{
this->a += widget.a;
printf("Widget operator +: this:%p widget:%p a:%d \n", this, &widget, a); return *this;
}
Widget getByCopy()
{
printf("Widget get object by copy: this:%p a:%d \n", this, a); return *this;
}
Widget& getByRef()
{
printf("Widget get object by ref: this:%p a:%d \n", this, a); return *this;
}
Widget* getByPointer()
{
printf("Widget get object by pointer: this:%p a:%d \n", this, a); return this;
} int a;
};
int _tmain(int argc, _TCHAR* argv[])
{
Widget w1(1); // 调用构造函数 Widget(int v)
Widget w2(2);
Widget w3; // 调用默认构造函数
w3 = (w1 + w2); // 先调用+操作符,在调用=操作符 printf("\n");
printf("w4 -------------------- \n");
w3.getByCopy(); // 返回副本
printf("w4_1 -------------------- \n");
Widget w4_1 = w3.getByCopy(); // 感觉应该会有两次拷贝,实际只有一次Widget(const Widget& widget)拷贝构造函数的调用,估计是编译器的优化
printf("w4_2 -------------------- \n");
Widget w4_2;
w4_2 = w3.getByCopy(); // 先调用Widget(const Widget& widget)拷贝构造函数,再调用=赋值拷贝函数
printf("w4_3 -------------------- \n");
Widget& w4_3 = w3.getByCopy(); // 调用一次Widget(const Widget& widget)拷贝构造函数
printf("-------------------- \n\n"); printf("w5 -------------------- \n");
w3.getByRef(); // 返回引用
printf("w5_1 -------------------- \n");
Widget w5_1 = w3.getByRef(); // 调用一次Widget(const Widget& widget)拷贝构造函数
printf("w5_2 -------------------- \n");
Widget w5_2;
w5_2 = w3.getByRef(); // 没有调用拷贝构造函数,调用一次=赋值拷贝函数
printf("w5_3 -------------------- \n");
Widget& w5_3 = w3.getByRef(); // 直接赋值给引用
printf("-------------------- \n\n"); printf("w6 -------------------- \n");
w3.getByPointer(); // 返回指针
printf("w6_1 -------------------- \n");
Widget w6_1 = w3.getByPointer(); // 调用一次Widget(const Widget* widget)拷贝构造函数
printf("w6_2 -------------------- \n");
Widget w6_2;
w6_2 = w3.getByPointer(); // 由于没有针对指针的赋值拷贝函数,先调用拷贝构造函数,创建一个临时变量,再调用=赋值拷贝函数
printf("w6_3 -------------------- \n");
Widget* w6_3 = w3.getByPointer(); // 直接赋值给指针
printf("-------------------- \n"); getchar(); return 0;
}

运行结果:

Widget constructor with parameters: this:0035FB70 a:1
Widget constructor with parameters: this:0035FB64 a:2
Widget constructor with default value: this:0035FB58 a:0
Widget operator +: this:0035FB70 widget:0035FB64 a:3
Widget operator =: this:0035FB58 widget:0035FB70 a:3 w4 --------------------
Widget get object by copy: this:0035FB58 a:3
Widget copy constructor with reference: this:0035F9FC widget:0035FB58 a:3
w4_1 --------------------
Widget get object by copy: this:0035FB58 a:3
Widget copy constructor with reference: this:0035FB4C widget:0035FB58 a:3
w4_2 --------------------
Widget constructor with default value: this:0035FB40 a:0
Widget get object by copy: this:0035FB58 a:3
Widget copy constructor with reference: this:0035FA08 widget:0035FB58 a:3
Widget operator =: this:0035FB40 widget:0035FA08 a:3
w4_3 --------------------
Widget get object by copy: this:0035FB58 a:3
Widget copy constructor with reference: this:0035FB28 widget:0035FB58 a:3
-------------------- w5 --------------------
Widget get object by ref: this:0035FB58 a:3
w5_1 --------------------
Widget get object by ref: this:0035FB58 a:3
Widget copy constructor with reference: this:0035FB1C widget:0035FB58 a:3
w5_2 --------------------
Widget constructor with default value: this:0035FB10 a:0
Widget get object by ref: this:0035FB58 a:3
Widget operator =: this:0035FB10 widget:0035FB58 a:3
w5_3 --------------------
Widget get object by ref: this:0035FB58 a:3
-------------------- w6 --------------------
Widget get object by pointer: this:0035FB58 a:3
w6_1 --------------------
Widget get object by pointer: this:0035FB58 a:3
Widget copy constructor with pointer: this:0035FAF8 widget:0035FB58 a:3
w6_2 --------------------
Widget constructor with default value: this:0035FAEC a:0
Widget get object by pointer: this:0035FB58 a:3
Widget copy constructor with pointer: this:0035FA14 widget:0035FB58 a:3
Widget operator =: this:0035FAEC widget:0035FA14 a:3
w6_3 --------------------
Widget get object by pointer: this:0035FB58 a:3
--------------------

  

C++ 中的返回值的更多相关文章

  1. Asp.net MVC 中Controller返回值类型ActionResult

    [Asp.net MVC中Controller返回值类型] 在mvc中所有的controller类都必须使用"Controller"后缀来命名并且对Action也有一定的要求: 必 ...

  2. [改善Java代码]不要在finally块中处理返回值

    在finally代码块中处理返回值,这是在面试题中经常出现的题目.但是在项目中绝对不能再finally代码块中出现return语句,这是因为这种处理方式非常容易产生"误解",会严重 ...

  3. Controller 中Action 返回值类型 及其 页面跳转的用法

        •Controller 中Action 返回值类型 View – 返回  ViewResult,相当于返回一个View 页面. -------------------------------- ...

  4. robot framework中的返回值

    1.若想要再setup中有返回值,给后续的操作使用 A)在setup的关键词中需要的返回值,设置为global variable或者suit variable:如下图:但是在编译器中,会报错,但是执行 ...

  5. Web API中的返回值类型

    WebApi中的返回值类型大致可分为四种: Void/ IHttpActionResult/ HttpResponseMessage /自定义类型 一.Void void申明方法没有返回值,执行成功后 ...

  6. c++中带返回值函数没写return能通过编译但运行时会出现奇怪问题

    c++中带返回值函数没写return能通过编译但运行时会出现奇怪问题 例如: string myFunc(){ theLogics(); } 发现调用: myFunc(); 崩溃. 但调用: cout ...

  7. try--catch--finally中return返回值执行的顺序(区别)

    1.try块中没有抛出异常,try.catch和finally块中都有return语句 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 public static int ...

  8. javascript学习笔记-2:jQuery中$("xx")返回值探究

    最近在写一个jQuery插件的时候,需要用到一个条件: 一组img标签,每一个元素都需要被它前面的元素值src替换,如果是第一个(序列为0)则其值为最后一个元素值,如果是最后一个,那么其值为第一个元素 ...

  9. try--catch--finally中return返回值执行的顺序

    1.try块中没有抛出异常,try.catch和finally块中都有return语句 public static int NoException(){ int i=10; try{ System.o ...

随机推荐

  1. TMS320C54x系列DSP的CPU与外设——第8章 流水线

    第8章 流水线 本章描述了TMS320C54x DSP流水线的操作,列出了对不同寄存器操作时的流水线延迟周期.(对应英语原文第7章) 8.1 流水线操作 TMS320C54x DSP有一个6段的指令流 ...

  2. 黄聪:VS2010每次F5启动都重新编译但是没办法进入断点

    这个问题主要是没有把Debug和Realse的生成配置勾上:

  3. 队列(Queue)--环形队列、优先队列和双向队列

    1. 队列概述 队列和堆栈都是有序列表,属于抽象型数据类型(ADT),所有加入和删除的动作都发生在不同的两端,并符合First In, First Out(先进先出)的特性. 特性: ·FIFO ·拥 ...

  4. angular2 的依赖注入

    angular2 的依赖注入包含了太多的内容,其中的一个重点就是注入器,而注入器又非常难理解,今天我们不深入介绍注入器的内容,可以参考官方文档,我们今天来说注入器的层级. 也就是组件获取服务的容器会选 ...

  5. MFC的类层次结构图

  6. codeforces 285 D. Permutation Sum 状压 dfs打表

    题意: 如果有2个排列a,b,定义序列c为: c[i] = (a[i] + b[i] - 2) % n + 1 但是,明显c不一定是一个排列 现在,给出排列的长度n (1 <= n <= ...

  7. hdu 5438 Ponds dfs

    Time Limit: 1500/1000 MS (Java/Others)     Memory Limit: 131072/131072 K (Java/Others) Problem Descr ...

  8. 转:谷歌大脑科学家 Caffe缔造者 贾扬清 微信讲座完整版

    [转:http://blog.csdn.net/buaalei/article/details/46344675] 大家好!我是贾扬清,目前在Google Brain,今天有幸受雷鸣师兄邀请来和大家聊 ...

  9. CSS根据屏幕分辨率应用相应样式

    当屏幕尺寸小于1200px时,应用下面的CSS样式 @media screen and (max-width: 1200px) { /*当屏幕尺寸小于1200px时,应用下面的CSS样式*/ .ind ...

  10. VVDocumenter-Xcode 规范注释生成器 插件之安装调试适应新版本

    1.下载地址: 很多时候,为了快速开发,很多的技术文档都是能省则省,这个时候注释就变得异常重要,再配合Doxygen这种注释自动生成文档的,就完美了.但是每次都要手动输入规范化的注释,着实也麻烦,但有 ...