Introduction to References
A reference is an alias, or an alternative name, for an existing variable. It provides a way to access the value of a variable indirectly through another name.
They do not have a memory address of their own. Instead they refer to the memory address of the variable they are referencing.
![code example](https://codefinity-content-media-v2.s3.eu-west-1.amazonaws.com/courses/38ac9b37-8877-4f9b-858b-b977f4305625/s3c1.png)
Now, referenceVariable is an alias for originalVariable. The primary purpose of references is to simplify and make safe, more readable code and avoid unnecessary memory overhead.
![](https://codefinity-content-media-v2.s3.eu-west-1.amazonaws.com/courses/38ac9b37-8877-4f9b-858b-b977f4305625/slider/efficiency.png)
![](https://codefinity-content-media-v2.s3.eu-west-1.amazonaws.com/courses/38ac9b37-8877-4f9b-858b-b977f4305625/slider/encrypted.png)
![](https://codefinity-content-media-v2.s3.eu-west-1.amazonaws.com/courses/38ac9b37-8877-4f9b-858b-b977f4305625/slider/readability.png)
![](https://codefinity-content-media-v2.s3.eu-west-1.amazonaws.com/courses/38ac9b37-8877-4f9b-858b-b977f4305625/slider/efficiency.png)
![](https://codefinity-content-media-v2.s3.eu-west-1.amazonaws.com/courses/38ac9b37-8877-4f9b-858b-b977f4305625/slider/encrypted.png)
![](https://codefinity-content-media-v2.s3.eu-west-1.amazonaws.com/courses/38ac9b37-8877-4f9b-858b-b977f4305625/slider/readability.png)
Advantages of Reference<br><br>
- Safe 🛡️: references cannot be null or uninitialized. Also they don't support arithmetic operations reducing the risk of accessing invalid memory;
- Memory Efficiency ⏱️: references avoid the need for copying large data structures, making code more memory-efficient;
- Readability 📖: Using references can make code more readable and concise, as they provide a clean way to work with variables;
- Modifiability 🔧: allow functions to modify the original values directly, providing a convenient way to update variables.
Все було зрозуміло?
Зміст курсу
C++ Pointers and References
3. References Fundamentals
C++ Pointers and References
Introduction to References
A reference is an alias, or an alternative name, for an existing variable. It provides a way to access the value of a variable indirectly through another name.
They do not have a memory address of their own. Instead they refer to the memory address of the variable they are referencing.
![code example](https://codefinity-content-media-v2.s3.eu-west-1.amazonaws.com/courses/38ac9b37-8877-4f9b-858b-b977f4305625/s3c1.png)
Now, referenceVariable is an alias for originalVariable. The primary purpose of references is to simplify and make safe, more readable code and avoid unnecessary memory overhead.
![](https://codefinity-content-media-v2.s3.eu-west-1.amazonaws.com/courses/38ac9b37-8877-4f9b-858b-b977f4305625/slider/efficiency.png)
![](https://codefinity-content-media-v2.s3.eu-west-1.amazonaws.com/courses/38ac9b37-8877-4f9b-858b-b977f4305625/slider/encrypted.png)
![](https://codefinity-content-media-v2.s3.eu-west-1.amazonaws.com/courses/38ac9b37-8877-4f9b-858b-b977f4305625/slider/readability.png)
![](https://codefinity-content-media-v2.s3.eu-west-1.amazonaws.com/courses/38ac9b37-8877-4f9b-858b-b977f4305625/slider/efficiency.png)
![](https://codefinity-content-media-v2.s3.eu-west-1.amazonaws.com/courses/38ac9b37-8877-4f9b-858b-b977f4305625/slider/encrypted.png)
![](https://codefinity-content-media-v2.s3.eu-west-1.amazonaws.com/courses/38ac9b37-8877-4f9b-858b-b977f4305625/slider/readability.png)
Advantages of Reference<br><br>
- Safe 🛡️: references cannot be null or uninitialized. Also they don't support arithmetic operations reducing the risk of accessing invalid memory;
- Memory Efficiency ⏱️: references avoid the need for copying large data structures, making code more memory-efficient;
- Readability 📖: Using references can make code more readable and concise, as they provide a clean way to work with variables;
- Modifiability 🔧: allow functions to modify the original values directly, providing a convenient way to update variables.
Все було зрозуміло?