引用的引用地址是否与指针的地址相同? [英] Is the address of a reference to a dereferenced pointer the same as the address of the pointer?

查看:126
本文介绍了引用的引用地址是否与指针的地址相同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中,引用的引用地址是否保证与指针的地址相同?

In C++, is the address of a reference to a dereferenced pointer guaranteed to be the same as the address of the pointer?

或者,下面的断言保证总是保持真的?

Or, written in code, is the following assertion guaranteed to always hold true?

SomeType *ptr = someAddress;
SomeType &ref = *ptr;
assert(&ref == ptr);


推荐答案

是的,当然, someAddress 不是空指针,否则不允许被取消引用。在这种情况下,行为是不确定的,虽然你的实现可能表现得好像是平等的,特别是在低优化级别。

Yes, provided of course that someAddress is not a null pointer, or otherwise not allowed to be dereferenced. In that case, behavior is undefined, although your implementation might well behave as though they are equal, especially with low optimization levels.

如果你想要精确, code>& ref 实际上不是引用的地址,它是引用的引用的地址。由于 ref 被绑定到 * ptr ,这意味着 ref ptr 的referand(或pointee如果你喜欢)是同一个对象,因此两个地址& ref ptr 是相等的。

If you want to be precise, then &ref isn't really the "address of a reference", it's the "address of the referand of a reference". Since ref was bound to *ptr, that means the referand of ref and the referand (or pointee if you prefer) of ptr are the same object, and hence the two addresses &ref and ptr are equal.

正如Bo指出的, c $ c>& ref with是指针的值或存储在指针中的地址,而不是指针的地址。

And as Bo points out, what you're comparing &ref with is the "value of the pointer", or the "address stored in the pointer", rather than "the address of the pointer".

这篇关于引用的引用地址是否与指针的地址相同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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