'&'和'*' (C++)的区别
来源:互联网 发布:linux监控工具 编辑:程序博客网 时间:2024/06/10 08:37
'&'和'*' (C++)的区别
文章作者:Prelude
It depends on what is meant by &. Since C++ makes use of overloaded built in operators, & can either take the address of an object, perform a bitwise AND operation, do anything related to a user defined type if overloaded by that type, or create a reference. In the context of this question, I'll assume a reference is meant.
A reference is an alternative name for an object, a synonym that refers to the same location in memory, but has a different name. The biggest difference between a pointer and a reference is that a reference need not be dereferenced.
Example of a reference:
int i = 1;
int& r = i; // i and r refer to the same int
r = 2; // i = 2
A pointer on the other hand, is a separate variable that contains a memory address as its value, that memory address can be dereferenced, or followed, to reach the contents of that memory address. The functionality of pointers are very similar to that of references:
int i = 1;
int *p = &i; // p now points to the address of i
*p = 2 // i = 2;
The biggest difference between references and pointers is that pointers may be treated like any other variable, operations can be performed on the pointer itself:
char *p = "Test";
p++; // p = "est";
References cannot have operations performed on them since they are not variables, they are synonyms for the referred object and any operations will act on that object, not the reference.
文章作者:Prelude
It depends on what is meant by &. Since C++ makes use of overloaded built in operators, & can either take the address of an object, perform a bitwise AND operation, do anything related to a user defined type if overloaded by that type, or create a reference. In the context of this question, I'll assume a reference is meant.
A reference is an alternative name for an object, a synonym that refers to the same location in memory, but has a different name. The biggest difference between a pointer and a reference is that a reference need not be dereferenced.
Example of a reference:
int i = 1;
int& r = i; // i and r refer to the same int
r = 2; // i = 2
A pointer on the other hand, is a separate variable that contains a memory address as its value, that memory address can be dereferenced, or followed, to reach the contents of that memory address. The functionality of pointers are very similar to that of references:
int i = 1;
int *p = &i; // p now points to the address of i
*p = 2 // i = 2;
The biggest difference between references and pointers is that pointers may be treated like any other variable, operations can be performed on the pointer itself:
char *p = "Test";
p++; // p = "est";
References cannot have operations performed on them since they are not variables, they are synonyms for the referred object and any operations will act on that object, not the reference.
- '&'和'*' (C++)的区别
- cpp和c的区别
- c和c++的区别?
- C和C++的区别
- C 和C++的区别
- C和C++的区别
- C和C++的区别
- C和C++的区别
- C中.和->的区别
- java和c的区别
- C和C++的区别
- C++和C的区别
- C和C++的区别
- C和C++的区别
- C和C++的区别:
- C++和C的区别
- C和C#的区别
- C中->和.的区别
- 制作最完美的GHOST纯净稳定版详细档案
- 病毒专杀工具自己编
- 你会用sizeof吗?(vc篇)
- 一个2000的日志清除器是怎么练成的
- Three ways to get your MAC address
- '&'和'*' (C++)的区别
- 得到枚举对象的对应值的下标
- Google 另类技巧不完全手册
- 鱼和水的故事
- 《代码大全》解读(四)
- 如何进步快
- CPU 主频,外频,倍频?前端总线?
- 第三十届国际大学生程序设计竞赛试题官方正式发布版
- WINPCAP翻译