Oliver
Oliver

Reputation: 3682

How to get a pointer from a reference?

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.

Upvotes: 88

Views: 97839

Answers (6)

Praetorian
Praetorian

Reputation: 109119

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

Upvotes: 138

Drew Dormann
Drew Dormann

Reputation: 63775

Use the address operator on the reference.

MyObject *ptr = &objRef;

Upvotes: 8

Joseph Mansfield
Joseph Mansfield

Reputation: 110658

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.

Upvotes: 47

Ben Voigt
Ben Voigt

Reputation: 283634

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.

Upvotes: 8

Kerrek SB
Kerrek SB

Reputation: 477040

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.

Upvotes: 30

damson
damson

Reputation: 2645

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.

Upvotes: -3

Related Questions