thed0ctor
thed0ctor

Reputation: 1396

how is *& being parsed as a function parameter in C++?

I'm trying to understand the different ways of passing references to pointers around. One implementation involves passing a pointer reference and the other involves passing the pointer.

I'm trying to understand how "*&" is being parsed in C++ in the latter.

Say I want to change what a pointer P points to. I can either pass the pointer or the reference to the pointer. If I pass a reference to the pointer my implementation would go something like this

void changePointer(int ** pp){

    //stuff that changes P from main();

}

//...

int main(){

    int a = 7;
    int * P = &a;
    changePointer(&P);
    return 0;

}

above, the parameter in changePointer is being parsed as:

void changePointer(int ** pp){
//int ** pp = &P; //where P is the integer pointer being passed by main
...

however if I wanted to pass the pointer, not its reference, then in main I would say:

//...
changePointer(P);
//...

and in changePointer I would change the parameter to:

void changePointer(int *& pp)

Now I have no clue how this is working or how to read this. Is this being parsed as:

int * pp = &P?

Upvotes: 1

Views: 923

Answers (5)

Armen Tsirunyan
Armen Tsirunyan

Reputation: 132994

int * & pp

is pronounced "pp is a reference to pointer to int". It means that the pointer argument is passed by reference meaning that any changes applied to it in the function will be reflected in the passed object, not its copy.

Upvotes: 1

template boy
template boy

Reputation: 10480

This is passing the pointer by reference just as any other object. Read the syntax from right to left: reference to a pointer. It would be the same as doing int *&pp = P just as it would be the same as doing int &j = i for simple integers.

void f(int *& ptr) {
    // f(pointer) is the same as
       int *& ptr = pointer
}

The way you create the argument is the same way it will be parsed; you're just creating a variable in the parameter that will be equal to the argument you pass to it. And references work the same way inside the function as they do outside. For example:

int main() {

    int a = 7,
        b = 5,
       *P = &a; // *P == 7

    int *&pp = P;

    pp = b; // *P == 5

}

Upvotes: 3

Mats Petersson
Mats Petersson

Reputation: 129364

It is parsed as (int *) (&) (pp).

The int * is just what it is, a pointer to integer. The & means "a reference to", and pp is the name of the variable.

What the compiler does when it has a reference parameter is to actually pass the address of the variable [or something that is similar to an address - this is implementation details in the compiler]. So ultimately, the compiler does exactly the same thing as &P in your first code-snippet - the only difference is that you can't be an idiot and pass a NULL to the function and thus cause the system to potentially crash.

Upvotes: 0

Luchian Grigore
Luchian Grigore

Reputation: 258598

This

void changePointer(int ** pp)

doesn't pass a pointer by reference - it passes a pointer to a pointer by value.

This

void changePointer(int *& pp)

passes a pointer by reference - changing the pointer inside the function will change the pointer itself outside.

Upvotes: 1

Cubic
Cubic

Reputation: 15673

int*& is a reference (&) to a pointer (*) to int.

Upvotes: 0

Related Questions