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. svn merge 回滚

    聊一聊 svn merge 命令. svn 是啥就不用介绍了吧,谁用谁知道.有了 svn,开发者只要把代码提交上去,无论山崩地裂.电脑进水.硬盘格式化,哪怕换了一台电脑,都能随时把代码找回来.不过从自 ...

  2. 【转】SQL SERVER 开窗函数简介

    在SQL SERVER 2005/2008支持两种排名开窗函数和聚集开窗函数. 以SQL SERVER中分面页为例,按时间顺序列出定单号. WITH OrderInfo AS ( SELECT ROW ...

  3. 深入分析Volatile的实现原理(转)

    引言 在多线程并发编程中synchronized和Volatile都扮演着重要的角色,Volatile是轻量级的synchronized,它在多处理器开发中保证了共享变量的“可见性”.可见性的意思是当 ...

  4. shell脚本摘要

    开启监听端口的程序时,查看是否开启成功(该例子监听8983端口) #动态显示[|][/][-][\] function spinner() { local pid=$1 local delay=0.5 ...

  5. HTTP头域列表与解释 之 request篇

    HTTP 头域是HTTP协议中请求(request)和响应(response)中的头部信息,其实就是HTTP通信的操作参数,告诉web服务器和浏览器怎样处理这个通信.HTTP头从一个请求信息或者响应信 ...

  6. Scala控制抽象

    private def filesHere = (new java.io.File(".")).listFiles() def filesEnding(query: String) ...

  7. All_从PO - INV - AP - SLA - GL重要数据表和接口程式(概念)

    2014-07-09 Created By BaoXinjian

  8. UVA 12563 劲歌金曲(01背包)

    劲歌金曲 [题目链接]劲歌金曲 [题目类型]01背包 &题解: 题意:求在给定时间内,最多能唱多少歌曲,在最多歌曲的情况下,使唱的时间最长. 该题类似于01背包问题,可用01背包问题的解题思路 ...

  9. POJ 3162 Walking Race 树形DP+线段树

    给出一棵树,编号为1~n,给出数m 漂亮mm连续n天锻炼身体,每天会以节点i为起点,走到离i最远距离的节点 走了n天之后,mm想到知道自己这n天的锻炼效果 于是mm把这n天每一天走的距离记录在一起,成 ...

  10. [Python] UTF-8最好不要带BOM

    一.问题回顾: 问题: 在写一个脚本读入IP分区表文件到list并做比较的时候,发现该成立的语句总是不成立,经调试后发现开头是这样:\xef\xbb\xbf1.0.3.0,故比较不成功. 解决办法:经 ...