Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Naumantamboli references #5545

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 91 additions & 25 deletions content/cpp/concepts/references/references.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
---
Title: 'References'
Description: '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 and aliases cannot be changed to alias something else.'
Title: "References"
Description: "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 and aliases cannot be changed to alias something else."
Subjects:
- 'Computer Science'
- 'Game Development'
- "Computer Science"
- "Game Development"
Tags:
- 'References'
- 'Pointers'
- 'Parameters'
- 'Memory'
- "References"
- "Pointers"
- "Parameters"
- "Memory"
CatalogContent:
- 'learn-c-plus-plus'
- 'paths/computer-science'
- "learn-c-plus-plus"
- "paths/computer-science"
---

A **reference** variable is an alias for another object. It is created using the `&` sign.
A reference variable is an alias for another object. It is created using the & sign.

Two things to note:

Expand All @@ -23,9 +23,9 @@ Two things to note:

## Syntax

```pseudo
pseudo
int &alias = varname;
```


## Pass-By-Reference

Expand All @@ -37,32 +37,98 @@ It allows the ability to:
- Avoid making copies of a variable/object for performance reasons.

```cpp

void swap_num(int &i, int &j) {
int temp = i;
i = j;
j = temp;
int temp = i;
i = j;
j = temp;
}

int main() {
int a = 100;
int b = 200;
int a = 100;
int b = 200;

swap_num(a, b);
swap_num(a, b);

std::cout << "A is " << a << "\n";
std::cout << "B is " << b << "\n";
std::cout << "A is " << a << "\n";
std::cout << "B is " << b << "\n";
}
```

## `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.
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.

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

## References with Classes

References can also be used with objects of classes. This allows modifying object properties directly by passing them as reference parameters.

```cpp
class Car {
public:
int speed;
Car(int spd) : speed(spd) {}

void setSpeed(int &newSpeed) {
speed = newSpeed;
}
};

int main() {
Car myCar(100);
int newSpeed = 200;
myCar.setSpeed(newSpeed);

std::cout << "The new speed is: " << myCar.speed << "\n";
}
int triple(int const &i) {
return i \* 3;
}
```

## Reference vs Pointer

References and pointers are similar in their usage but have key differences:
References are aliases for variables and cannot be reseated (i.e., they cannot be made to refer to another object after initialization).
Pointers, on the other hand, store the memory address of variables and can be reassigned to point to different memory locations.

```cpp
int main() {
int a = 10;
int b = 20;
int &ref = a; // Reference to 'a'
int *ptr = &a; // Pointer to 'a'
ref = b; // This modifies 'a' (not the reference itself)
ptr = &b; // This changes the pointer to point to 'b'
std::cout << "Reference points to: " << a << "\n"; // 20
std::cout << "Pointer now points to: " << *ptr << "\n"; // 20
}
```

## Modifying Arrays Using References

Passing arrays by reference is also useful for avoiding unnecessary copies, especially for large datasets.

```cpp
void modifyArray(int (&arr)[5]) {
for (int i = 0; i < 5; i++) {
arr[i] = arr[i] \* 2;
}
}

int main() {
int myArr[5] = {1, 2, 3, 4, 5};
modifyArray(myArr);

for (int i = 0; i < 5; i++) {
std::cout << myArr[i] << " ";
}
}
```