Initialization of references and variables in C ++

Given the following C ++ function:

int& ReturnAReference() { /* Do something here */ } 

Is there a difference between the two statements:

 int normalVariable = ReturnAReference(); int& referenceVariable = ReturnAReferene(); 

Is one version preferred over another?

+4
source share
4 answers

Regarding this:

 int normalVariable = ReturnAReference(); 

normalVariable is an integer and is assigned an int value that ReturnAReference() refers to. Since such an increment, assignment, or implementation of something else for normalVariable will not affect what ReturnAReference() has internally.

Regarding this:

 int& referenceVariable = ReturnAReference(); 

referenceVariable is a reference to an integer that would otherwise be internal to ReturnAReference() . Since such an increment, assignment, or execution of something else for referenceVariable will affect what ReturnAReference() has internally.

Which is preferable depends on what you are trying to accomplish, but in many cases the second approach (using referenceVariable ) violates the "encapsulation" ( http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) ), which is considered bad .

EDIT: And I have to add that if ReturnAReference () returns a reference to a variable that is local in this function, that reference will be invalid as soon as ReturnAReference () returns.

+9
source

After initializing the link, for example. via

 int i = 42; int& r1 = i; int& r2 = ReturnAReference(); int& r3(i); int& r4{i}; // C++11 int& r5 = {i}; // C++11 

it becomes an alias, that is, a different name for the object with which it was initialized. This is not another integer. Note that in the C ++ standard language, an object is simply a storage area, not necessarily an instance of a class.

As a link is an alias, if you work with a link, you will work with the original object (the one that was initialized):

 int i = 42; int& r = i; // r is now an alias for i, both refer to the same object r = 21; // a true assignment std::cout << i; // will print 21 

Statement

 int normalVariable = ReturnAReference(); 

introduces a new object of type int and the name for this object: normalVariable . This object is initialized with the object returned by ReturnAReference() , which means that the value of the returned object is copied to a new object called normalVariable .

Statement, on the other hand

 int& referenceVariable = ReturnAReferene(); 

introduces a new name for the object returned by ReturnAReference() .


If your function returns a link without an int reference, for example int ReturnAnInt(); operator

 int& r = ReturnAnInt(); 

will become illegal, since the object returned by this function is temporary, which lives only until the end of this line (in this case). In the next line, the name r will refer to an object that no longer exists, so it was made illegal to link non-constant references to temporary objects.

+1
source

Suppose you have the following definitions:

 int test = 4; int& ReturnAReference() { return test; } 

1) Is there a difference between the two statements:

 int normalVariable = ReturnAReference(); 

In this case, normalVariable will contain a copy of the return value (not a reference), since the assignment operator copies the value indicated by the return value to normalVariable . This means that after

 normalVariable = 1; 

normalVariable will now be 1, but test will still be 4.

However, if you wrote

 int& referenceVariable = ReturnAReferene(); 

and do

 normalVariable = 1; 

normalVariable will now be 1 and test will also be 1, since normalVariable is only an alias for test .

Be careful when returning the link. For example, if you want to do

 int& ReturnAReference() { int i = 0; return i; } 

the link returned from ReturnAReference() will no longer be valid, since it is valid only inside the function and will be destroyed when it exits.

2) Is one version preferable over another?

In the case of int or other primitive types, I would prefer an int return value over int& , simply because int is small and will not be expensive to copy (it will almost always be case-sensitive). In addition, the return value of int& entails security issues if the reference refers to a local variable. For classes or structures, this always depends, but you should be careful when returning local variables by reference or pointer.

+1
source

If you want to change the variable returned by the link from the function, or to track any changes in its value, use int &. It is up to you to make sure that the variable you are referencing will exist as long as you receive it. If you're just curious about the value of the variable returned by the link at this point, use int.

The functions FYI, std :: vector operator [] and at returned by reference, allowing syntax such as the following v.at(0) = 2 .

0
source

Source: https://habr.com/ru/post/1497722/


All Articles