Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use reference type alias with pointer operator to declare a reference to pointer?

I have a simple example here: I used type alias using using keyword for a reference type then I want to know whether I can use that type alias with pointer operator(*) to declare a reference to pointer:

int main(){

    using ref_int = int&;

    int x = 10;
    int* p = &x;

    //int*(&rpx) = p;
    //ref_int * rptrx = p; // pointer to reference is not allowed.
    *ref_int(rptrx) = p; // rptrx is undefined

}
  • Because as a matter of curiosity when I used the Element-type of std::vector<int>::reference I wanted to combine it with pointer operator * to declare a reference to pointer:

    int* ptr = new int(1000);
    std::vector<int>::*(reference rptr) = ptr; // error: expected expression
    
  • But I can to use pointer type alias combined with reference operator "&" to declare it:

    using pInt = int*;
    
    int i = 57;
    int* ptrI = &i;
    pInt(&rpInt) = ptrI;
    
    cout << *rpInt << endl;
    

** I know I cannot have a pointer to a reference because a reference is just an alias name for an already existing object while a pointer is an object thus we can have a pointer or a reference to it.

like image 482
Maestro Avatar asked Aug 05 '19 12:08

Maestro


People also ask

Can alias be given to pointers?

In C++, references are just aliases for the thing that they refer to, the standard doesn't even require them to take up any storage. Trying to use a reference alias to make a reference to a pointer will not work because using the alias will only ever give you a pointer to a reference type.

Which operator is used to reference a pointer?

To assign an address of a variable into a pointer, you need to use the address-of operator & (e.g., pNumber = &number ). On the other hand, referencing and dereferencing are done on the references implicitly.

Can you make a reference to a pointer?

References to pointers can be declared in much the same way as references to objects. A reference to a pointer is a modifiable value that's used like a normal pointer.

Can we create reference to pointer in C++?

Note: It is allowed to use “pointer to pointer” in both C and C++, but we can use “Reference to pointer” only in C++. If a pointer is passed to a function as a parameter and tried to be modified then the changes made to the pointer does not reflects back outside that function.


1 Answers

You can't have a pointer to a reference in C++. In C++, references are just aliases for the thing that they refer to, the standard doesn't even require them to take up any storage. Trying to use a reference alias to make a reference to a pointer will not work because using the alias will only ever give you a pointer to a reference type.

So, if you want a pointer to the thing the reference refers to you just use

auto * ptr = &reference_to_thing;

If you want a reference to a pointer that syntax is

int foo = 42;
int* ptr = &foo;
int*& ptr_ref = ptr;
like image 77
NathanOliver Avatar answered Oct 22 '22 11:10

NathanOliver