C++ Const Reference

Опубликовано: 21 Январь 2025
на канале: Kenny Yip Coding
1,433
55

Const Reference in C++. In C++, there is a term called const correctness which is the practice of adding const when not modifying values. In C++, when we create temporary variables such as function parameters or the variable used for ranged base for loops, we create a copy of the value. This can be expensive for non primitive data types such as strings and vectors where we copy over every element in the collections. Therefore it is better to use a reference and if not modifying the values, a const reference. Because of how references are implemented, it is actually more efficient to just copy the primitive data types.

If you need to review these topics:
Functions:    • C++ Functions  
Pass by Value vs Reference:    • C++ Functions Pass by Value or Reference  

C++ Playlist:
   • C++ Tutorial  

Install C++ with VS Code:
   • How to set up C++ in Visual Studio Code  

Subscribe for more coding tutorials 😄!