C++ 如何从引用中获取指针?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9263658/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 12:33:53  来源:igfitidea点击:

How to get a pointer from a reference?

c++

提问by Oliver

There seems to be many relavent questions talking about pointer vs. reference, but I couldn't find what I want to know. Basically, an object is passed in by a reference:

似乎有很多关于指针与引用的相关问题,但我找不到我想知道的。基本上,对象是通过引用传入的:

funcA(MyObject &objRef) { ... }

Within the function, can I get a pointer to that object instead of the reference? If I treat the reference objRefas an alias to the MyObject, would &objRefactually give me a pointer to the MyObject? It doesn't seem likely. I am confused.

在函数中,我可以获得指向该对象的指针而不是引用吗?如果我将引用objRef视为 的别名MyObject&objRef实际上会给我一个指向 MyObject 的指针吗?似乎不太可能。我很迷惑。

Edit: Upon closer examination, objRefdoes give me back the pointer to object that I need - Most of you gave me correct info/answer, many thanks. I went along the answer that seems to be most illustrative in this case.

编辑:经过仔细检查,objRef确实给了我指向我需要的对象的指针 - 你们中的大多数人给了我正确的信息/答案,非常感谢。在这种情况下,我采用了似乎最能说明问题的答案。

回答by Praetorian

Yes, applying the address-of operator to the reference is the same as taking the address of the original object.

是的,将 address-of 运算符应用于引用与获取原始对象的地址相同。

#include <iostream>

struct foo {};

void bar( const foo& obj )
{
  std::cout << &obj << std::endl;
}

int main()
{
  foo obj;
  std::cout << &obj << std::endl;
  bar( obj );

  return 0;
}

Result:

结果:

0x22ff1f
0x22ff1f

回答by Joseph Mansfield

Any operator applied to a reference will actually apply to the object it refers to (§5/5 [expr]); the reference can be thought of as another name for the same object. Taking the address of a reference will therefore give you the address of the object that it refers to.

应用于引用的任何运算符实际上都将应用于它所引用的对象(§5/5 [expr]);可以将引用视为同一对象的另一个名称。因此,获取引用的地址将为您提供它所引用的对象的地址。

It as actually unspecified whether or not a reference requires storage (§8.3.2/4 [dcl.ref]) and so it wouldn't make sense to take the address of the reference itself.

实际上未指定引用是否需要存储(§8.3.2/4 [dcl.ref]),因此获取引用本身的地址是没有意义的。

As an example:

举个例子:

int x = 5;
int& y = x;
int* xp = &x;
int* yp = &y;

In the above example, xpand ypare equal - that is, the expression xp == ypevaluates to truebecause they both point to the same object.

在上面的例子中,xpand ypare equal - 也就是说,表达式的xp == yp计算结果为,true因为它们都指向同一个对象。

回答by Kerrek SB

The general solution is to use std::addressof, as in:

一般的解决方案是使用std::addressof,如:

#include <type_traits>

void foo(T & x)
{
    T * p = std::addressof(x);
}

This works no matter whether Toverloads operator&or not.

无论是否T过载,这都有效operator&

回答by Ben Voigt

Use the address-of (&) operator on the reference.

&在引用上使用 address-of ( ) 运算符。

&objRef

Like any other operator used on a reference, this actually affects the referred-to object.

与在引用上使用的任何其他运算符一样,这​​实际上会影响被引用的对象。

As @Kerrek points out, since the operator affects the referred-to object, if that object has an overloaded operator&function, this will call it instead and std::address_ofis needed to get the true address.

正如@Kerrek 指出的那样,由于操作符会​​影响被引用的对象,如果该对象具有重载operator&函数,则会调用它,std::address_of并且需要获取真实地址。

回答by Drew Dormann

Use the address operator on the reference.

在引用上使用地址运算符。

MyObject *ptr = &objRef;

回答by damson

In C++, a reference is a restricted type of pointer. It can only be assigned once and can never have a NULL value. References are most useful when used to indicate that a parameter to a function is being Passed by Reference where the address of the variable is passed in. Without a Reference, Pass By Value is used instead.

在 C++ 中,引用是一种受限制的指针类型。它只能分配一次,并且永远不能有 NULL 值。当用于指示函数的参数通过引用传递,其中传递变量的地址时,引用最有用。如果没有引用,则使用传递值。