Key Concepts

Review core concepts you need to learn to master this subject

const Reference

int triple(int const &i) { return i * 3; }

In C++, pass-by-reference with const can be used for a function where the parameter(s) won’t change inside the function.

This saves the computational cost of making a copy of the argument.

References and Pointers
Lesson 1 of 1
  1. 1
    A computer’s memory is a sequence of bytes. We can number the bytes from 0 to the last one. Each number, known as an address, represents a location in the …
  2. 2
    In C++, a reference variable is an alias for something else, that is, another name for an already existing variable. So suppose we make Sonny a reference to someone named Songqiao. You can refe…
  3. 3
    So what’s a good use case for references? Let’s take a look. Previously, when we passed parameters to a function, we used normal variables and that’s known as pass-by-value. But because the vari…
  4. 4
    Remember const? The const keywords tells the compiler that we won’t change something. For example, in the following code, we are telling the compiler that the double variable pi will stay at 3.14 …
  5. 5
    So we have learned about references (aliases), which are created by using the & symbol in a variable declaration. But the & sign can have another meaning. The “address of” operator, &, is used to …
  6. 6
    In C++, a pointer variable is mostly the same as other variables, which can store a piece of data. Unlike normal variables, which store a value (such as an int, double, char), a pointer stores a …
  7. 7
    So now we learned what a pointer is and how to create one, but is there a way to obtain the value pointed to by the pointer? The asterisk sign * a.k.a. the dereference operator is used to obtain t…
  8. 8
    When we declare a pointer variable like so, its content is not intialized: int* ptr; In other words, it contains an address of “somewhere”, which is of course not a valid location. This is [dang…
  9. 9
    Congratulations! You have officially done it. 🎉🎉🎉 In this lesson, you have learned: - References - Pass-by-reference - Pass-by-reference with const - Memory addresses and how to access them -…

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo