当前位置:网站首页>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
边栏推荐
- 2021-09-28
- Project manager's thinking mode worth trying: project success equation
- Domain driven model DDD (III) -- using saga to manage transactions
- The prefix of static of egg can be modified, including boots
- Study notes: unity customsrp-10-point and spot shadows
- Study notes: unity customsrp-11-post processing --- bloom
- The 2021 IT industry project management survey report was released!
- Laravel routing settings
- Solution of how to log in with mobile phone verification code in wireless network
- Interpretation of common SQL statements
猜你喜欢

双击.jar包无法运行解决方法

Camera imaging + homography transformation + camera calibration + stereo correction

Uncle wolf is looking for a translator -- Plato -- ongoing translation

Use of uniapp native plug-ins

Laravel implements the Holy Grail model with template inheritance

Box collapse and margin collapse

Kanban Quick Start Guide

JS array common methods

Create a tabbar component under the components folder, which is public

2021-10-12
随机推荐
相机成像+单应性变换+相机标定+立体校正
点击添加按钮--出现一个框框(类似于添加学习经历-本科-研究生)
Study notes: unity customsrp-10-point and spot shadows
2021-10-08
d.ts---更详细的知识还是需要看官网的介绍(声明文件章节)
(11) Vscode code formatting configuration
可执行程序执行流程
Laravel implements the Holy Grail model with template inheritance
What role do tools play in digital transformation?
CORS and proxy (づ  ̄ 3  ̄) in egg ~ the process of stepping on the pit and filling the pit ~ tot~
Tslint annotations ignore errors and restful understanding
Use of ES6 array
2021-09-23
日志简介和构建web应用
Three 之 three.js (webgl)简单实现根据点绘制线/弧线(基于LineGeometry / Line2 / LineMaterial,绘制两点基于圆心的弧线段)
Three of three JS (webgl) simple sorting of rotation attribute function, and a simple case of rotating around the axis based on this
[the background color changes after clicking a line]
JS time format conversion
Excel 2016 cannot open the file for the first time. Sometimes it is blank and sometimes it is very slow. You have to open it for the second time
My old programmer's perception of the dangers and opportunities of the times?