[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 ...
随机推荐
- jeecg中树形显示的用法
1.GoodsController中显示的方法如下: @RequestMapping(params = "goodsgrid") @ResponseBody public Obje ...
- laravel里面使用event
模式:大概是通过一个自定义的event,一个handler,还有一个binder,然后用来简化通知模型 生成自定义的event ./artisan make:event MyEvent 生成自定义的h ...
- (转)jdbc 调用 sql server 的存储过程时“该语句没有返回结果集”的解决方法
本文转载自:http://hedyn.iteye.com/blog/856040 在JDBC中调用SQL Server中的存储过程时出现如下异常: com.microsoft.sqlserver.jd ...
- http 和 https 区别?
1. HTTP 的URL 以http:// 开头,而HTTPS 的URL 以https:// 开头2. HTTP 是不安全的,而 HTTPS 是安全的3. HTTP 标准端口是80 ,而 HTTPS ...
- small_vector
folly/small_vector.h folly::small_vector<T,Int=1,...> is a sequence container that implements ...
- ArraySort--冒泡排序、选择排序、插入排序工具类demo
public class ArraySort { private long[] a; private int nElems; public ArraySort(int max){ a=new long ...
- hdu1010 Tempter of the Bone(深搜+剪枝问题)
Tempter of the Bone Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Submission( ...
- PHP mysql client封装
config1.inc.php $CONFIG_DATABASE_TXL = array( #array('127.0.0.1', 'root', '', 'he_txl','3306') array ...
- WebStorm开发TypeScript的设置
Webstorm IDE可以开发TypeScript,同时支持自动编译成js文件,下面我们来进行一些简单的配置. 1.去node.js官网下载安装node.js 2.下载安装新版本的Webstorm ...
- springboot-shiro chapter01——创建springboot webmvc环境
简介:本章提供了springboot简单例子,主要包含以下内容 1.pom.xml依赖资源 2.springboot配置 3.web应用spring mvc 环境: IDEA15+ JDK1.8+ M ...