Is there any difference in the following two?
const int64 x = some_struct.x;
const int64& x = some_struct.x;
Is one better than the other? I have recently seen the reference being used somewhere but could not understand why someone would do that.
You can think reference as an alias of the original variable.
some_struct.x = 1;
const int64 x1 = some_struct.x;
const int64& x2 = some_struct.x;
std::cout << x1 << "," << x2 << std::endl; // should be "1,1"
some_struct.x = 2;
std::cout << x1 << "," << x2 << std::endl; // should be "1,2"
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments