博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c++引用
阅读量:4695 次
发布时间:2019-06-09

本文共 1155 字,大约阅读时间需要 3 分钟。

首先我们知道任何变量都有左值和右值,分别对应变量的内存区域和内存区域中的值

引用其实就是变量的别名,其实是一个东西有两个名吧

定义方式 类型 &引用名 = 变量名 而且必须定义的时候就初始化,不然是错误的

看个例子吧

1 #include
2 using namespace std; 3 4 int main() 5 { 6 int i = 9; 7 int *p = &i; 8 int &ir = i; 9 cout << &i << " " << &ir << " " << p << endl;10 11 cout << i << " " << *p << " " << ir << endl;12 cout << &i << " " << &ir << " " << p << endl;13 14 *p = 3;15 cout << i << " " << *p << " " << ir << endl;16 cout << &i << " " << &ir << " " << p << endl;17 18 ir = 8;19 cout << i << " " << *p << " " << ir << endl;20 cout << &i << " " << &ir << " " << p << endl;21 22 i = 99;23 cout << i << " " << *p << " " << ir << endl;24 cout << &i << " " << &ir << " " << p << endl;25 26 int *p2 = &ir;27 cout << i << " " << *p << " " << ir << " " << *p2 << endl;28 29 *p2 = 666;30 cout << &i << " " << &ir << " " << p << " " << p2 << endl;31 cout << i << " " << *p << " " << ir << " " << *p2 << endl;32 cout << &i << " " << &ir << " " << p << " " << p2 << endl;33 return 0;34 }

 

转载于:https://www.cnblogs.com/mch5201314/p/11568699.html

你可能感兴趣的文章
Error和Exception
查看>>
Python和Singleton (单件)模式[转载]
查看>>
httpclient设置proxy与proxyselector
查看>>
IT常用单词
查看>>
拓扑排序
查看>>
NYOJ--32--SEARCH--组合数
查看>>
JMS
查看>>
gulpfile 压缩模板
查看>>
【34.14%】【BZOJ 3110】 [Zjoi2013]K大数查询
查看>>
【 henuacm2016级暑期训练-动态规划专题 A 】Cards
查看>>
第五篇:白话tornado源码之褪去模板的外衣
查看>>
设备常用框架framework
查看>>
bootstrap模态框和select2合用时input无法获取焦点(转)
查看>>
21世纪经济网APP
查看>>
解决NetworkOnMainThreadException
查看>>
1039 到底买不买
查看>>
农银电商项目学习笔记(一)
查看>>
MockObject
查看>>
Chukwa
查看>>
(转)Maven仓库——私服介绍
查看>>