This question has already been answered:

In some sources I have heard that links are just a wrapper over pointers. Those. These are pointers, but they automatically receive the address of the variable and dereference themselves to get the value. Other sources say that a link is just a second name for a variable and it does not take a single byte of memory. I myself hold a second opinion, but I would like to know for sure. So what are links?

Reported as a duplicate by participants Alexey Shimansky , Community Spirit ♦ Jul 30 '17 at 10:10 pm .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • Something tells me that the first definition was invented only to explain how the links work, and has nothing to do with the actual implementation - yrHeTaTeJlb
  • Hooked on the link - it's just the second name for the variable and it does not take a single byte of memory . How then is the transfer by reference? without a single byte of memory? Link to dynamically allocated variable? - Harry
  • 3
    Within the use of the link, no additional pointer is created, but if we pass the link to the function, it is identical to the pointer transfer. The first and second definition do not contradict each other. - ixSci
  • The link takes memory in a specific case or not - the implementation decides. The expression of the reference through the pointer is used when the pointers have already been studied and it is necessary to familiarize the person with the links in a simple way. Both pointers and links operate with addresses, but these are different entities and they do it differently. - Croessmah
  • This is not a duplicate question. - Isin Min

1 answer 1

The C ++ reference allows you to create an alias for variables in the program. It can not be uninitialized, unlike the pointer. Both definitions are correct, but the first is written in some language for "dummies" can be misleading. There is more http://cppstudio.com/post/429/

Run a piece of code. To better understand.

int number = 1; int& ref = number; // Π‘ΠΎΠ·Π΄Π°Ρ‚ΡŒ ссылку cout << "ΠŸΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½Π°Ρ number содСрТит " << number << endl; cout << "Π”Ρ€ΡƒΠ³ΠΎΠ΅ имя содСрТит " << ref << endl; ref += 500; // измСняСм Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½ΠΎΠΉ посрСдством измСнСния значСния Π² ссылкС cout << "ΠŸΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½Π°Ρ number содСрТит " << number << endl; cout << "ПсСвдоним для number содСрТит " << ref << endl;