为什么无法取得参考地址 [英] Why it is not possible to take address of reference

查看:67
本文介绍了为什么无法取得参考地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我做了很多尝试并搜索了许多页面,但我仍然想知道为什么我们不能获取参考地址.有人建议它是其他对象的别名,那又如何呢?它必须存储在某个内存位置,不是吗.

有人可以解释吗?

问候

Hi,

I tried a lot and search many pages, but I still wonder why we cannot take address of reference. Some suggest it is alias for some other object, so what? It must be stored at some memory location, isn''t it.

Can anyone explain it?

Regards

推荐答案

您为什么认为我们不能?我们可以,但是结果地址将与被引用对象的地址相同.没错,这是对象名称的别名.您也可以将对象的名称视为参考.难怪对象和别名具有相同的地址:

Why do you think we cannot? We can, but the resulting address would be the same as the address of the referenced object. That''s correct, this is the alias of the name of the object. You consider the name of the object as a reference, too. No wonder that the object and the alias have the same address:

int object;
int & refObject = object; // important: there is no way to reassign it to a different object later
int * pRefObject = &refObject;
int * pObject = &object;
// from this point, pRefObject == pObject;

// you can give an name to the object having no name,
// such as dereferenced pointer:

// at this point, the object pObject carries no information on the fact
// that it points to the object named "object" 
// this is how you can named it:
int & namedObject = *pObject; // now it can be used anywhere the name "object" could be used
// (referential transparency, please see the very last link below)



您可能会尝试将引用视为是另一个对象,该对象与被引用对象不同,但这不是引用.这不是另一种指针(但是,例如,.NET引用实质上是尽管通过通过引用"传递参数的语法非常相似;这只是一个不同但相似的概念,用相同的名称表示) ).只是一个提示:所有这些因果关系的东西主要是为了抽象出l值并模拟复杂对象的值语义"而发明的.

请参阅:

http:///publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8l.doc%2Flanguage%2Fref%2Fcplr110.htm [ http://en.wikipedia.org/wiki/C%2B%2B_reference [ ^ ],
http://www.infernodevelopment.com/simple-c-pointers-and-references [ ^ ],
http://www.parashift.com/c++-faq-lite/references.html [ ^ ],

http://en.wikipedia.org/wiki/Value_%28computer_science%29 [ ^ ].

—SA



You probably try to consider a reference as yes another object, different from the referenced object, but this is not what the reference is. This is not another kind of a pointer (but, for example, .NET reference is, essentially, despite of a great similarity in syntax of passing a parameter "by reference"; this is just a different but similar concept denoted by the same name). Just a hint: all this casuistic stuff is primarily invented to abstract out l-values and simulate "value semantic" for complex objects.

Please see:

http://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8l.doc%2Flanguage%2Fref%2Fcplr110.htm[^],

http://en.wikipedia.org/wiki/C%2B%2B_reference[^],
http://www.infernodevelopment.com/simple-c-pointers-and-references[^],
http://www.parashift.com/c++-faq-lite/references.html[^],

http://en.wikipedia.org/wiki/Value_%28computer_science%29[^].

—SA


这篇关于为什么无法取得参考地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆