注册 登录
中国红客联盟 返回首页

毁灭_perish的个人空间 http://www.cnhonkerarmy.com/?837563 [收藏] [复制] [分享] [RSS]

日志

'&'和'*' (C++)的区别

热度 2已有 982 次阅读2016-7-25 11:34

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.

发表评论 评论 (1 个评论)

回复 Ariza 2018-3-20 00:01

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 注册

红盟社区--中国红客联盟 

Processed in 0.069339 second(s), 8 queries.

站点统计| 举报| Archiver| 手机版| 黑屋 |   

Powered by HUC © 2001-2017 Comsenz Inc.

手机扫我进入移动触屏客户端

关注我们可获取更多热点资讯

Honor accompaniments. theme macfee

返回顶部