'The difference between the two initialization?

What are the differential factors between these two transformation initialization constructs?

int main()
{
     VectorXYZ NewLocation = VectorXYZ(30, 30, 0);
     SetLocation(NewLocation);

// and

     SetLocation(VectorXYZ(30, 30, 0));
}


Solution 1:[1]

If the function SetLocation is declared for example like

void SetLocation( VectorXYZ & );

then you may it call like

SetLocation(NewLocation);

but may not call it like

 SetLocation(VectorXYZ(30, 30, 0));

because you may not bind a temporary object with a non constant lvalue reference.

On the other hand, if the function is declared like

void SetLocation( VectorXYZ && );

then you may call it like

 SetLocation(VectorXYZ(30, 30, 0));

but you may not call it like

SetLocation(NewLocation);

If the function is declared like

void SetLocation( const VectorXYZ & );

or like

void SetLocation( VectorXYZ );

then the both calls are valid.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Vlad from Moscow