[C++] Returning values by reference in C++
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++的更多相关文章
- Returning Values from Bash Functions
转自:https://www.linuxjournal.com/content/return-values-bash-functions Bash functions, unlike function ...
- [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 ...
- 4.1 primitive and reference values
ECMAScript variables may contains two different types of data: primitive values and reference values ...
- 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 ...
- 1.【使用EF Code-First方式和Fluent API来探讨EF中的关系】
原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/relationship-in-entity-framework-using-code-firs ...
- [转载]两个半小时学会Perl
Learn Perl in about 2 hours 30 minutes By Sam Hughes Perl is a dynamic, dynamically-typed, high-leve ...
- 菜鸟学习 - Unity中的热更新 - LuaInterface用户指南
[由于学习,所以翻译!] 1.介绍 LuaInterface 是 Lua 语言和 Microsoft.NET 平台公共语言运行时 (CLR) 之间的集成库. 非常多语言已经有面向 CLR 编译器和 C ...
- 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 ...
- [转]Whirlwind Tour of ARM Assembly
ref:http://www.coranac.com/tonc/text/asm.htm 23.1. Introduction Very broadly speaking, you can divid ...
随机推荐
- win7环境下,golang thrift demo代码编译不通过
用官方的教程代码:http://thrift.apache.org/tutorial/go 用网友提供的代码:Golang RPC 之 Thrift 都出现如下情况 状况1: 编辑器中就会提醒 Can ...
- 搭建基于hyperledger fabric的联盟社区(六) --搭建node.js服务器
接下来我要做的是用fabric sdk来做出应用程序,代替CLI与整个区块链网络交互.并且实现一个http API,向社区提供一个简单的接口,使社区轻松的与区块链交互. 官方虽然提供了Node.JS, ...
- 紫金桥OPC接口使用技巧
OPC接口使用技巧 OPC接口是由OPC基金会制定的,基于DCOM技术的,用于控制系统软件之间进行数据通讯的接口规范.由于其开放性和高效性,现在已被广泛应用于自动化控制领域及生产信息管理中.紫金桥软件 ...
- ESXI5-WIN2008R2安装域控以及额外域笔记
每次安装域控都要找教程,每次都没法一次性搞定.写个笔记吧...主要是给自己看的.写的比较含糊.不清楚的可以直接QQ本人. 1.安装WIN2008R2,192.168.188.10 2.上载SLICET ...
- 支付宝吱口令自动复制脚本,自动复制 JavaScript 代码介绍
本文转自:http://www.sojson.com/blog/262.html 最近支付宝#吱口令#的信息随处可见,可谓是铺天盖地,群里发这样的信息给被踢了不少.我开始还在鄙视这些人,有几个小钱?然 ...
- abbreviation
1. ps------process status 2. tty-----teletype 3. ping----packet internet groper 4. nohup-----no hang ...
- MySql入门(1)
环境变量的重要性环境变量是在操作系统中一个具有特定名字的对象,它包含了一个或者多个应用程序所将使用到的信息.例如Windows和DOS操作系统中的path环境变量,当要求系统运行一个程序而没有告诉它程 ...
- Oracle IO问题解析(转)
http://www.hellodba.com/reader.php?ID=76〈=cn 数据库的作用就是实现对数据的管理和查询.任何一个数据库系统,必然存在对数据的大量读或者写或者两中操作都大量存在 ...
- 第十章 消息驱动的微服务: Spring Cloud Stream
Spring Cloud Stream 是一个用来为微服务应用构建消息驱动能力的框架. 它可以基于Spring Boot 来创建独立的. 可用于生产的 Spring 应用程序. 它通过使用 Sprin ...
- swoole学习
<?php ini_set('default_socket_timeout', -1); class serverEmail { public $serv = null; public func ...