C++ having reference for local variables

Undivided

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.

songyuanyao

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Local variables definition in relation to scope, C++

From Dev

Immutable local 'variables' in C#

From Dev

C fundamentals - Having problems with variables and pointers

From Dev

Mocking local variables in C#

From Dev

C# Returning local variables

From Dev

Scope and lifetime of local variables in C

From Dev

C++ global and local variables

From Dev

Is it possible to do variables with local scoping in c#?

From Dev

Coding in C: efficiency of temporary local variables

From Dev

C# Func: access local variables in a block

From Dev

Difference between local and temporal variables in C++

From Dev

Assigns clause for local variables in Frama-C

From Dev

C++ 11 - rvalue reference variables

From Dev

Lambdas and capture by reference local variables : Accessing after the scope

From Dev

Why C allows uninitialized local variables?

From Dev

Alterations to reference variables in c++

From Dev

Does the C++ standard force capture-by-reference of local variables to be inefficient?

From Dev

Rvalue reference and auto&& local variables

From Dev

Local Variables Being Passed ( C++)

From Dev

Do local reference variables add to the GC

From Dev

C fundamentals - Having problems with variables and pointers

From Dev

Scope and lifetime of local variables in C

From Dev

Immutable local 'variables' in C#

From Dev

C local and global static variables

From Dev

Passing local variable by reference to a C++ thread

From Dev

how to reference local variables on the stack properly

From Dev

C# having problems with using global variables

From Dev

C++ Remote - Having files local and on the host

From Dev

Null reference on local variable unity c#

Related Related

HotTag

Archive