DMarlon Studio

Lesson 13 · 13 of 20

References and const references · C++

See the difference between passing by value and by reference (&) in C++, and use const & to pass vectors and strings without copying them.

C++ · References and const references in DMarlon Studio: the example's code and its output in the terminal

By default C++ passes arguments by value: the function gets a copy. addBonusCopy adds 50 to its copy, which reaches 150, but score is still 100.

With `&` after the type, the parameter is a reference: another name for the original variable. addBonus(int& points) works on score itself, which goes up to 150.

A reference can also be a variable: int& alias = score; creates nothing new, so alias = 0 sets score to 0. A reference stays tied to the same variable from the moment it is created.

Copying a number costs nothing, but copying a vector or a long string does. `const std::vector<double>&` avoids the copy and also promises not to change the original: if total tried to modify prices, the program would not compile. It is the usual way to pass strings and vectors.

In a range-based for, double& p lets you change each element, so the 10% discount ends up applied to basket. For reading only, use const double& p.

Code · main.cpp

#include <iostream>#include <vector>// By value: the function gets a copy, so the original does not change.void addBonusCopy(int points) { points += 50; std::cout << "Inside the copy:     " << points << '\n'; }// By reference (&): the function works on the original variable.void addBonus(int& points) { points += 50; }// const reference: no copy and no changes. Ideal for strings and vectors.double total(const std::vector<double>& prices) {    double sum = 0;    for (double p : prices) sum += p;    return sum;}int main() {    int score = 100;    addBonusCopy(score);    std::cout << "After the copy:      " << score << '\n';    addBonus(score);    std::cout << "After the reference: " << score << '\n';    int& alias = score;  // a reference: another name for the same variable    alias = 0;    std::cout << "alias = 0, so score is now " << score << '\n';    // In a for, & changes each element in place: a 10% discount.    std::vector<double> basket = {20.0, 12.5, 7.5};    for (double& p : basket) p *= 0.9;    for (const double& p : basket) std::cout << p << ' ';    std::cout << "\nTotal after discount: " << total(basket) << '\n';    return 0;}

Output · c++ -std=c++17 main.cpp -o main && ./main

Inside the copy:     150
After the copy:      100
After the reference: 150
alias = 0, so score is now 0
18 11.25 6.75 
Total after discount: 36

Key points

  • By value, the function works on a copy.
  • With type&, it works on the original.
  • const type& avoids the copy and forbids changes.
  • for (auto& x : v) changes the vector's elements.
  • Pass strings and vectors as const & when you only read them.

Exercise

Write void applyVat(std::vector<double>& prices), which adds 21% to each price, and double maxPrice(const std::vector<double>& prices), and try them on the basket.