A C++ program can be made easier to read and maintain by using references rather than pointers. A C++ function can return a reference in a similar way as it returns a pointer.

When a function returns a reference, it returns an implicit pointer to its return value. This way, a function can be used on the left side of an assignment statement. For example, consider this simple program:

#include <iostream>
#include <ctime> using namespace std; double vals[] = {10.1, 12.6, 33.1, 24.1, 50.0}; double& setValues( int i )
{
return vals[i]; // return a reference to the ith element
} // main function to call above defined function.
int main ()
{ cout << "Value before change" << endl;
for ( int i = ; i < ; i++ )
{
cout << "vals[" << i << "] = ";
cout << vals[i] << endl;
} setValues() = 20.23; // change 2nd element
setValues() = 70.8; // change 4th element cout << "Value after change" << endl;
for ( int i = ; i < ; i++ )
{
cout << "vals[" << i << "] = ";
cout << vals[i] << endl;
}
return ;
}
 

When the above code is compiled together and executed, it produces the following result:

Value before change
vals[] = 10.1
vals[] = 12.6
vals[] = 33.1
vals[] = 24.1
vals[] =
Value after change
vals[] = 10.1
vals[] = 20.23
vals[] = 33.1
vals[] = 70.8
vals[] =

When returning a reference, be careful that the object being referred to does not go out of scope. So it is not legal to return a reference to local var. But you can always return a reference on a static variable.

int& func() {
int q;
//! return q; // Compile time error
static int x;
return x; // Safe, x lives outside this scope
}

[C++] Returning values by reference in C++的更多相关文章

  1. Returning Values from Bash Functions

    转自:https://www.linuxjournal.com/content/return-values-bash-functions Bash functions, unlike function ...

  2. [Go] Returning Multiple Values from a Function in Go

    Returning multiple values from a function is a common idiom in Go, most often used for returning val ...

  3. 4.1 primitive and reference values

    ECMAScript variables may contains two different types of data: primitive values and reference values ...

  4. MySQL 5.6 Reference Manual-14.2 InnoDB Concepts and Architecture

    14.2 InnoDB Concepts and Architecture 14.2.1 MySQL and the ACID Model 14.2.2 InnoDB Multi-Versioning ...

  5. 1.【使用EF Code-First方式和Fluent API来探讨EF中的关系】

    原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/relationship-in-entity-framework-using-code-firs ...

  6. [转载]两个半小时学会Perl

    Learn Perl in about 2 hours 30 minutes By Sam Hughes Perl is a dynamic, dynamically-typed, high-leve ...

  7. 菜鸟学习 - Unity中的热更新 - LuaInterface用户指南

    [由于学习,所以翻译!] 1.介绍 LuaInterface 是 Lua 语言和 Microsoft.NET 平台公共语言运行时 (CLR) 之间的集成库. 非常多语言已经有面向 CLR 编译器和 C ...

  8. Java – 4 Security Vulnerabilities Related Coding Practices to Avoid---reference

    This article represents top 4 security vulnerabilities related coding practice to avoid while you ar ...

  9. [转]Whirlwind Tour of ARM Assembly

    ref:http://www.coranac.com/tonc/text/asm.htm 23.1. Introduction Very broadly speaking, you can divid ...

随机推荐

  1. send函数和recv函数

    1.send 函数 int send( SOCKET s, const char FAR *buf, int len, int flags );   不论是客户还是服务器应用程序都用send函数来向T ...

  2. GOF23设计模式之原型模式(prototype)

    一.原型模式概述 1.通过new产生一个对象需要非常繁琐的数据准备和访问权限,则可以使用原型模式. 2.就是java中的克隆技术,以某个对象为原型,复制出新的对象,显然,新的对象具备原型对象的特点. ...

  3. windows7下怎样安装whl文件(python)

    本文转载自:http://blog.csdn.net/fhl812432059/article/details/51745226 windows7 python2.7 1.用管理员方式打开cmd 2. ...

  4. Linux性能监控 - CPU、Memory、IO、Network

    一.CPU 良好状态指标 CPU利用率:User Time <= 70%,System Time <= 35%,User Time + System Time <= 70%. 上下文 ...

  5. 《C++ Primer》读书笔记

    在C++中,基类必须指出希望派生类重新定义哪些函数,定义为virtual的函数是基类期待派生类重新定义的,基类希望派生类继承的函数不能定义为虚函数. 引用和指针的静态类型与动态类型可以不同,这是C++ ...

  6. 关于标签的属性-<a>

    标签的属性可以分成两个大类 1.系统属性名:例如 id class src这些都是系统里自带的 2.自定义属性名:可以根据使用的需要自行定义 下面我们简短介绍一下<a>标签的使用 < ...

  7. Web页面工作流设计器

    http://www.cnblogs.com/2018/archive/2011/11/22/2240259.html http://wenku.baidu.com/link?url=LSqlCiqi ...

  8. Oracle中的dbms_metadata.get_ddl的用法

    当我们想要查看某个表或者是表空间的DDL的时候,可以利用dbms_metadata.get_ddl这个包来查看. dbms_metadata包中的get_ddl函数详细参数 GET_DDL函数返回创建 ...

  9. 为什么 ReactJS 不适合复杂的前端项目?

    问题一:ReactJS组件难以在复杂交互页面中复用 ReactJS中的最小复用单位是组件.ReactJS的组件比AngularJS的Controller和View 要轻量些. 每个组件只需要前端开发者 ...

  10. SSD固态盘应用于Ceph集群的四种典型使用场景

    在虚拟化及云计算技术大规模应用于企业数据中心的科技潮流中,存储性能无疑是企业核心应用是否虚拟化.云化的关键指标之一.传统的做法是升级存储设备,但这没解决根本问题,性能和容量不能兼顾,并且解决不好设备利 ...