prosseek
prosseek

Reputation: 190889

double & and move function in C++

I would like to use parameter in C++ to store back whatever value/object. In this example, I try to store the value from the global variable as a simplified example.

This code doesn't work,

int value = 20;

void returnPointer2(int* hello)
{
    hello = &value;
}

// It changes nothing 
int value2 = 100; 
returnPointer2(&value2);
cout << value2 << endl;

as I needed double pointer.

void returnPointer3(int** hello)
{
    *hello = &value;
}

int* vp2 = new int();
*vp2 = -30;  
returnPointer3(&vp2);
cout << *vp2 << endl; // expects 20

I reminded of the reference, and I can use pointer reference to get the same result.

void returnPointer4(int* & hello)
{
    cout << "value : " << value;
    hello = &value;
}

int* vp3 = new int();
*vp3 = -130;  
returnPointer4(vp3); // also expects 20, but much simpler to use
cout << "better : " << *vp3 << endl;

I tried with double &, and it compiles.

void returnPointer5(int&& hello)
{
    cout << "value : " << value;
    hello = value;
}

However, it doesn't compile with the input of integer variable.

int vp4 = 123; 
returnPointer5(vp4); // also expects 20, but even more simpler to use
cout << "best : " << vp4 << endl;

This is an error message.

pointer_return.cpp:31:6: error:   initializing argument 1 of 'void returnPointer5(int&&)'
void returnPointer5(int&& hello)

I happened to know about move, and it works with this code.

int vp4 = 123; 
returnPointer5(move(vp4)); // also expects 20, but much simpler to see
cout << "best : " << vp4 << endl;

What's the magic/logic behind this move function?

Upvotes: 0

Views: 908

Answers (3)

user1810087
user1810087

Reputation: 5334

The magic of std::move is:

The actual declaration for std::move is somewhat more involved, but at its heart, it's just a static_cast to an rvalue reference.

Taken from here.

As Jeffery Thomas already said, a && is not a reference to a reference, but a reference to a rvalue.

Upvotes: 1

Jeffery Thomas
Jeffery Thomas

Reputation: 42588

There is a lot of stuff getting mixed in here, but to keep it simple I'll address your root question.

&& is nothing at all like **.

&& is an rvalue reference, while ** is a pointer to a pointer.


As a second point, you are declaring in your function name what you want to do: returnPointer4.

You want to have a pointer to an integer returned back. int*& is the correct syntax for having a reference to a pointer.


Reading over your question again, why don't you use the following:

int& returnGlobalReference() {
    return value;
}

Then in your other function:

int& value2 = returnGlobalReference();

Upvotes: 2

Raja
Raja

Reputation: 3106

The first attempt makes the classic mistake of passing a pointer by value, modifying its address in the function and expecting what it points to to change.

As mentioned in the comments,

void returnPointer2(int* hello)
{
    hello = &value; // don't do this, it only modifies what the 
                    // pointer hello, which resides in the stack, points to

    *hello = value; // do this instead. even though hello still resides in the                  
                    // stack, you're modifying the location that hello points to,
                    // which was your original intention

}

why do you want to pass pointers however? is the static variable not available when you call the function? (perhaps, different files?)

Upvotes: 1

Related Questions