Key Concepts
Review core concepts you need to learn to master this subject
References
References
In C++, a reference variable is an alias for another object. It is created using the &
sign. Two things to note:
- Anything done to the reference also happens to the original.
- Aliases cannot be changed to alias something else.
Pass-By-Reference
Pass-By-Reference
In C++, pass-by-reference refers to passing parameters to a function by using references.
It allows the ability to:
- Modify the value of the function arguments.
- Avoid making copies of a variable/object for performance reasons.
const
Reference
const
Reference
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.
Memory Address
Memory Address
In C++, the memory address is the location in the memory of an object. It can be accessed with the “address of” operator, &
.
Given a variable porcupine_count
, the memory address can be retrieved by printing out &porcupine_count
. It will return something like: 0x7ffd7caa5b54
.
Pointers
Pointers
In C++, a pointer variable stores the memory address of something else. It is created using the *
sign.
Dereference
Dereference
In C++, a dereference reference operator, *
, can be used to obtain the value pointed to by a pointer variable.
- 1A 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 …
- 2In 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…
- 3So 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…
- 4Remember 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 …
- 5So we haved 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…
- 6In 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 …
- 7So 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…
- 8When 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…
- 9Congratulations! 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
How you'll master it
Stress-test your knowledge with quizzes that help commit syntax to memory