Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pointer to Reference or Reference to Pointer

Tags:

c++

I just asked myself what is the proper syntax of References to Pointers and/or Pointers to References. Also is there any difference between them?

int*& x1;
int&* x2;

EDIT:

int*& valid;
int&* wrong;
like image 818
nonsensation Avatar asked Jun 11 '26 12:06

nonsensation


1 Answers

One makes sense, the other doesn't. You can only form pointer types based on object and function types, or on void. Since a reference is none of those, there is no type "pointer to reference".

Here's another perspective: A pointer stores the address of an object or function. A reference variable isn't an object, though, it merely refers to an object. You don't take the address of a variable, but the address of an object.

like image 186
Kerrek SB Avatar answered Jun 14 '26 02:06

Kerrek SB



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!