2

Can anyone make me clear what these lines of code are doing? Is the value of m being copied to the memory location of n? Or n is also pointing to the same address as m? And how?

int m=5;
int &n=m;
dbush
  • 186,650
  • 20
  • 189
  • 240
  • It says "'m' and 'n' are different names for the same thing". – molbdnilo Mar 09 '22 at 13:41
  • 1
    While you ask what both lines are doing, I'm reading into your question that it's mostly about the meaning of the specific line `int &n = m;`. Does this answer your question? https://stackoverflow.com/questions/46969031/when-is-a-reference-variable-appropriate-and-why-can-you-explain-the-actual-syn Or this? https://stackoverflow.com/questions/2765999/what-is-a-reference-variable-in-c – Drew Dormann Mar 09 '22 at 13:42
  • @molbdnilo can you explain how? – pratik neupane Mar 09 '22 at 13:45
  • @DrewDormann that post is quite complex I guess ..can you explain that in your own words? – pratik neupane Mar 09 '22 at 13:45
  • 2
    You know how "My name is William, but you can call me Bill" works? Same thing. – molbdnilo Mar 09 '22 at 13:45
  • 2
    Does this answer your question? [What is a reference variable in C++?](https://stackoverflow.com/questions/2765999/what-is-a-reference-variable-in-c) – prapin Mar 09 '22 at 13:46
  • The `&` here makes `n` a reference to an `int` and `int &n = m` makes it so that `n` is essentially just an alias for `m`. What this actually compiles to will depend on what happens in the rest of the code. If it can be statically determined that `n` always points to this `m` then essentially `n` doesn't exist after compilation (no memory used for it etc.). In other scenarios, if what `n` points to is determined in run-time, then it will be something similar to a pointer (so some form of storage will most likely be required). – odyss-jii Mar 09 '22 at 13:47
  • @odyss-jii thanks sir I thought this had something to do with the memory locations as pointers do hehe – pratik neupane Mar 09 '22 at 13:52
  • 1
    `m` is a [C++ Object](https://en.cppreference.com/w/cpp/language/object), it has size, storage, lifetime etc. `n` is __not__ a C++ Object; the declaration `int &n=m;` is just an instruction the to the compiler to treat any use of `n` as __an alias__ for `m`. There is no requirement for the compiler to use any memory to store `n` (although it can if needs to). – Richard Critten Mar 09 '22 at 14:12

2 Answers2

1
int m = 5;

The above line makes a new variable of type int and assigns it the value 5.

int &n = m;

This makes a reference variable of type int and assigns m to it. This means that n is basically another name for m, and if n's value is changed, m's value will change as well. It's kinda like pointers, but way more high level than pointers.

Solved Games
  • 1,361
  • 3
  • 16
1
int m = 5;

This line creates a variable of type int which contains the value 5.

int& n = m;

This line creates a reference to m. You can think of it as an alias to m. So this instruction :

n = 6;

Actually changes the value of m.

References are typically (not always though, see here) implemented as pointers internally. The compiler will typically reserve a location in memory where it will store the memory adress of m. This means that the following pieces of code are equivalent:

int main() {
    int m = 5;
    int& n = m; // Reference
    n = 6;
}
int main() {
    int m = 5;
    int* const n = &m; // Pointer const (meaning the held memory address 
                       // can't be changed, only the value at that address can be)
    *n = 6;
}

I personally only use const references, meaning I can't modify the value of the values that is being referenced. When I actually want to modify the referenced value, I use pointers because I think they provide more clarity (they basically say "I'm not owning this value, which means that when I modify it, expect a value somewhere else in the program to have changed.").

So when to use const references? Arguably their most frequent use case is passing a function argument by reference. For example, passing a std::string object to the following function actually copies it, which can be very expensive for large objects.

#include <string>

void doStuffWithString(std::string str /* potentially expensive */) {
    // ...
}

Instead, you can pass it by const reference, meaning you don't pass the actual object to the function, but a const reference to it to prevent the unnecessary copy.

#include <string>

void doStuffWithString(const std::string& str /* not expensive */) {
    // ...
}

If I ever want to modify the value of the object being passed, I pass it by address (with a pointer).

#include <string>

void modifyString(std::string* str /* pointer */) {
    // ...
}

int main() {
    std::string hiMom("Hi mom!");
    modifyString(&hiMom); // Explicitly shows the caller that we're passing
                          // a pointer, meaning the value of hiMom might change
    return 0;
}
Debaug
  • 324
  • 2
  • 13