0

Possible Duplicate:
C++ Reference, change the refered variable

I know that references in c++ are just pointers that get dereferenced for you when you use them. This question is about how to access the underlying pointer and change it.

Consider this code:

int x;
int& x_ref = x;              //now equivalent to x
int* x_ptr = &x;         //stores address of x
int* x_ref_ptr = &x_ref; //ALSO stores address of x

int&* x_ref_ptr_ref = ???; //what would this mean?

I'm trying to change where a reference points after initialization. I have no concern for type safety or proper practices. Does the c++ language have any tool to let me accomplish this?

Community
  • 1
  • 1
Filipp
  • 1,843
  • 12
  • 26
  • 1
    What you "know" may not actually be true. Pointers are pointers, and references are references. They're different things. – Kerrek SB Sep 21 '12 at 11:27

2 Answers2

5

There is no pointer to reference, it's ill-formed. A reference is an alias to an object. How would a pointer to an alias work?

Also, it's a feature of the language that a reference can't be reseated. A reseatable reference is a pointer.

Xeo
  • 129,499
  • 52
  • 291
  • 397
0

This is not possible by design. By using a reference instead of a pointer, you decide to never change its target after declaration, with all entailing drawbacks and advantages (one of which is its "automatic dereferencing". Read the Wikipedia entry on references carefully.

You will need to switch to pointers.

SvenS
  • 795
  • 7
  • 15
  • That article says that typical compilers implement this internally as a pointer. Still 'by design' answers my question. – Filipp Sep 26 '12 at 17:59