C ++ beginner: what is the point of using a variable by reference when using "const"?

I was interested to learn about the logic in the declaration of this function:

CMyException (const std::string & Libelle = std::string(),... 

What is the point of using a variable by reference? Usually you pass a variable by reference when it can be changed internally ... so if you use the const keyword, it means that it will never be changed.

This is controversial.

Can someone explain this to me?

+4
source share
6 answers

In fact, the link is used to avoid an unnecessary copy of the object.

Now, to understand why const used, try the following:

 std::string & x= std::string(); //error 

This will give a compilation error. This is because the expression std::string() creates a temporary object that cannot be bound to a non-constant reference. However, the temporary can be bound to a const reference, so const is required:

 const std::string & x = std::string(); //ok 

Now back to the constructor in your code:

 CMyException (const std::string & Libelle = std::string()); 

It sets the default value for the parameter. The default value is created from a temporary object. Therefore, you need const (if you use the link).

There is also the advantage of using a const reference: if you have such a constructor, you can throw an exception like this:

 throw CMyException("error"); 

It creates a temporary object of type std::string from the string literal "error" , and this temporary is associated with a const reference.

+5
source

Some arguments may use quite some memory. If you pass the argument as a value, it will be copied and a copy will be passed to the method.

Passing them as a link will only result in a pointer to the method, which is faster, and will save memory for the copy.

+3
source

For example, you can only do this with const reference argumentmernt:

 CMyException("foo"); 

Think about it, and then it will become clear.

+1
source

Passing primitive types ( int , char , ..) using a const reference does not make sense. Even for std::string I would say that this is not necessary.

However, large structures or classes require a copy when passing by value, so there is overhead. The const reference mimics the behavior of the transfer by value (the external variable does not change), but also prevents an additional copy.

+1
source

Not. It is not always necessary that whenever you pass a variable as a reference, it can only be changed inside it. If a variable is passed by value, then a copy of the variable is made whenever this function is called.

On the other hand, the reference variable uses the same object and essentially passes only the memory address (just like with std :: string *, but with the exception that you cannot use the zero memory address). So, when you do something like const std::string& x , you say:

 1. The passed argument will not be copied. The same object will be used as in memory. 2. The function will absolutely not modify the object that it is handling. 

If you think about it, using const makes sense when you work with links, not otherwise. If you make a copy of the variable that I pass in and then change it, I still don't need to. However, it would be very useful to know that if you are not going to modify the object that I am transmitting (since you will use the same object), I can strictly define the process of my application in accordance with this guarantee.

0
source

I generally pass everything as const - I never change the parameters, by value for primitive types, by reference for custom types. Passing by value for primitive types is more efficient in most cases - consider unsigned short - by value 2 bytes, by reference 4-8 bytes depending on the size of the pointer.

0
source

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


All Articles