How to get a pointer from a reference?

C++

C++ Problem Overview


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 objRef as an alias to the MyObject, would &objRef actually give me a pointer to the MyObject? It doesn't seem likely. I am confused.

Edit: Upon closer examination, objRef does 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.

C++ Solutions


Solution 1 - C++

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

#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

Solution 2 - C++

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.

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.

As an example:

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

In the above example, xp and yp are equal - that is, the expression xp == yp evaluates to true because they both point to the same object.

Solution 3 - C++

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

#include <type_traits>

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

This works no matter whether T overloads operator& or not.

Solution 4 - C++

Use the address operator on the reference.

MyObject *ptr = &objRef;

Solution 5 - C++

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

&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_of is needed to get the true address.

Solution 6 - C++

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionOliverView Question on Stackoverflow
Solution 1 - C++PraetorianView Answer on Stackoverflow
Solution 2 - C++Joseph MansfieldView Answer on Stackoverflow
Solution 3 - C++Kerrek SBView Answer on Stackoverflow
Solution 4 - C++Drew DormannView Answer on Stackoverflow
Solution 5 - C++Ben VoigtView Answer on Stackoverflow
Solution 6 - C++damsonView Answer on Stackoverflow