当前位置:网站首页>The main difference between pointer and reference
The main difference between pointer and reference
2022-04-23 05:27:00 【What’smean】
The pointer “ Point to ” An object in memory , And quote “ Bound to the ” An object in memory , They all implement indirect access to other objects , There are two main differences between the two :
First of all , The pointer itself is an object , Allows assignment of values and values to pointers , And in the life cycle of the pointer, it can point to several different objects ; Reference is not an object , Cannot rebind reference to another object .
second , The pointer does not need to have an initial value at definition time , Like other built-in types , If a pointer defined within the scope of a block is not initialized , Will also have an uncertain value ; The reference must be given an initial value at the time of definition .
The code is :
#include<iostream>
using namespace std;
void swap1(int &a,int &b){ // Reference implementation
int temp=0;
temp = a;
a = b;
b = temp;
};
void swap2(int *a,int *b){ // Pointer implementation
int temp=0;
temp = *a;
*a = *b;
*b = temp;
};
int main(){
int i=5,j=10;
int*p=&i;
cout<<p<<"\t"<<*p<<endl;
p=&j;
cout<<p<<"\t"<<*p<<endl;
*p=20;
cout<<p<<"\t"<<*p<<endl;
j=30;
cout<<p<<"\t"<<*p<<endl;
swap1(i,j);
cout<<i<<"\t"<<j<<endl;
cout<<p<<"\t"<<*p<<endl;
swap2(&i,&j);
cout<<i<<"\t"<<j<<endl;
cout<<p<<"\t"<<*p<<endl;
return 0;
}
Output results :
0x66ff24 5
0x66ff20 10
0x66ff20 20
0x66ff20 30
30 5
0x66ff20 5
5 30
0x66ff20 30
Please press any key to continue . . .
版权声明
本文为[What’smean]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220544124497.html
边栏推荐
- WTL self drawn control library (cqscheckcomboxbox)
- 世界与个人发展
- (11) Vscode code formatting configuration
- 2021-11-08
- 【openh264】cmake: msopenh264-static
- Wbpack configuring production development environment
- Interpretation of common SQL statements
- 开源规则引擎——ice:致力于解决灵活繁复的硬编码问题
- Three 之 three.js (webgl)旋转属性函数的简单整理,以及基于此实现绕轴旋转的简单案例
- Three of three JS (webgl) simple sorting of rotation attribute function, and a simple case of rotating around the axis based on this
猜你喜欢

If I am PM's performance, movie VR ticket purchase display

Domain driven model DDD (III) -- using saga to manage transactions

Three of three JS (webgl) simple sorting of rotation attribute function, and a simple case of rotating around the axis based on this
![[untitled]](/img/49/770888f4f351f42af0e01c3a15ddfa.png)
[untitled]

phphphphphphphp

Open source rule engine - Ice: dedicated to solving flexible and complex hard coding problems

varnish入门

我这位老程序员对时代危险和机遇的一点感悟?

Three 之 three.js (webgl)简单实现根据点绘制线/弧线(基于LineGeometry / Line2 / LineMaterial,绘制两点基于圆心的弧线段)
![[triangle Yang Hui triangle printing odd even cycle JS for break cycle]](/img/9a/6cdc00e6056a1a47d2fbb8b9a8e975.png)
[triangle Yang Hui triangle printing odd even cycle JS for break cycle]
随机推荐
2021-09-23
Data management of basic operation of mairadb database
egg测试的知识大全--mock、superTest、coffee
Three methods of list rendering
The source of anxiety of graduating college students looking for technology development jobs
Devops life cycle, all you want to know is here!
CORS and proxy (づ  ̄ 3  ̄) in egg ~ the process of stepping on the pit and filling the pit ~ tot~
Multi process model in egg -- egg document Porter
Log introduction and building web application
创建进程内存管理copy_mm - 进程与线程(九)
The introduction of lean management needs to achieve these nine points in advance
Simple and basic use of switch and if
Study notes: unity customsrp-10-point and spot shadows
Graphics.FromImage报错“Graphics object cannot be created from an image that has an indexed pixel ...”
The 2021 IT industry project management survey report was released!
Multiple mainstream SQL queries only take the latest one of the data
TSlint注释忽略错误和RESTful理解
Anti crawler (0): are you still climbing naked with selenium? You're being watched! Crack webdriver anti crawler
If I am PM's performance, movie VR ticket purchase display
(11) Vscode code formatting configuration