文档章节

C++之引用

SVD
 SVD
发布于 2015/11/07 15:05
字数 422
阅读 50
收藏 1

In the C++ programming language, a reference is a simple reference datatype that is less powerful but safer than the pointer type inherited from C. The name C++ reference may cause confusion, as in computer science a reference is a general concept datatype, with pointers and C++ references being specific reference datatype implementations. The definition of a reference in C++ is such that it does not need to exist. It can be implemented as a new name for an existing object (similar to rename keyword in Ada).

C++ references differ from pointers in several essential ways:

It is not possible to refer directly to a reference object after it is defined; any occurrence of its name refers directly to the object it references.

Once a reference is created, it cannot be later made to reference another object; it cannot be reseated. This is often done with pointers.

References cannot be null, whereas pointers can; every reference refers to some object, although it may or may not be valid. Note that for this reason, containers of references are not allowed.

References cannot be uninitialized. Because it is impossible to reinitialize a reference, they must be initialized as soon as they are created. In particular, local and global variables must be initialized where they are defined, and references which are data members of class instances must be initialized in the initializer list of the class's constructor. 

References can be thought of as "Symbolic links" in file system terminology. Symbolic links can be modified as though they were the file they were linked to and when a symbolic link is deleted the original file remains. Similarly, references can be deleted (whether by going out of scope or explicitly being removed if they were allocated in heap) and the original object that was referenced remains. Similarly, once a symbolic link has been created it can never be changed.

#include <iostream>
#include <stdlib.h>
using namespace std;

void fun(int &a, int &b);

int main(void)
{
	int x = 10;
	int y = 20;
	cout << x << "," << y << endl;
	fun(x, y);
	cout << x << "," << y << endl;
	system("pause");
	return 0;

}
void fun(int &a, int &b)
{
	int c = 0;
	c = a;
	a = b;
	b = c;
}

程序利用引用实现了数据的互换。引用可以简单理解为是变量的一个别名,对别名的操作同样也会作用到原变量之上。

© 著作权归作者所有

共有 人打赏支持
上一篇: C++之 const
下一篇: namespace
SVD

SVD

粉丝 34
博文 207
码字总数 102940
作品 0
海淀
私信 提问
C++中extern “C”含义深层探索

1.引言 C++语言的创建初衷是“a better C”,但是这并不意味着C++中类似C语言的全局变量和函数所采用的编译和连接方式与C语言完全相同。作为一种欲与C兼容的语言,C++保留了一部分过程式语言...

长平狐
2013/01/06
38
0
C++中extern “C”含义深层探索

首先,作为extern是C/C++语言中表明函数和全局变量作用范围(可见性)的关键字,该关键字告诉编译器,其声明的函数和变量可以在本模块或其它模块中使用。 通常,在模块的头文件中对本模块提供...

Jerikc
2014/04/24
0
1
C++ 对象资源管理惯用法

原文:C++ 对象资源管理惯用法 作者:Breaker 关于 C++ 对象资源管理的惯用法,note-to-self + keynote + idiom case + cross-reference 式笔记 keyword: RAII, deleter, Two-stage Initiali...

晨曦之光
2012/05/23
147
0
C# vs C++之二:GC vs RAII

C# vs C++之二:GC vs RAII 资源管理 C中资源管理极为繁琐易错,大多复杂C系统都面临内存泄露、悬挂指针等问题 一方面由底层语言特点决定;另一方面也由于C语言特性相对较少,严重依赖程序员...

ddatsh
2011/06/28
1K
6
4-C++远征之起航篇-学习笔记

c++教程起航篇 我们会讲C++那些事,C++与C语言的关系。 C++诞生于贝尔实验室。 C++之父: 本贾尼·斯特劳斯特卢普 C++社区排行榜 最新排行,c++排名第三,Python排名第四 C++语言的应用领域:...

天涯明月笙
2018/07/20
0
0

没有更多内容

加载失败,请刷新页面

加载更多

OSChina 周五乱弹 —— 看来我只适合当一个千斤顶

Osc乱弹歌单(2019)请戳(这里) 【今日歌曲】 @小小编辑:推荐歌曲《我想我是伟大的》 《我想我是伟大的》- 九天 手机党少年们想听歌,请使劲儿戳(这里) 最近面试的好多呀, 面试了一个漂...

小小编辑
23分钟前
194
9
grafana安装使用及与zabbix集成

grafana简介 Grafana是一个完全开源的度量分析与可视化平台,可对来自各种各种数据源的数据进行查询、分析、可视化处理以及配置告警。 Grafana支持的数据源: 官方:Graphite,InfluxDB,Ope...

阿dai学长
56分钟前
12
0
带你看数据挖掘与机器学习-厦大EDP上课出勤预测

带你看数据挖掘与机器学习-厦大EDP上课出勤预测 标签: 数据挖掘 特征工程 机器学习 出勤预测 write by xmhexi 2019/3/22 内容提要 首先说明本文是一篇科普文章,通过一个实际案例,帮助理解什...

xmhexi
今天
128
0
IOS  学习记录

1.StackView=>IOS 9及以上支持 2.布局方式: AutoLayout / StackView 堆布局 (线性布局) 3.屏幕适配 (资源分辨率、设计分辨率、屏幕分辨率) Size Class技术 可以针对 屏幕的方向进行设置...

萨x姆
今天
5
0
第四次工业革命:自主经济的崛起

https://36kr.com/p/5170370.html

shengjuntu
昨天
5
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部