References play a role similar to a pointer, but the address to which it refers is immutable. When you touch a reference, you’re not doing anything but tinkering with the variable it refers to, it never changes. They are equivalent, but should be used according to the cases in which they are requested, performing their core functions. There is also a specific type of references that behave differently, called tree reference. It is used when the life time of variable is small.
What also changes is the way the compiler will treat the variable you have, syntactic sugar and good practices. This is very general, this way, so let’s look at an example. If you have a function int f(int&)
, you can enter both with a int&
or with a *(int*)
, correct - in these cases, they necessarily complement each other. Because this is the basic, a concept that arose from C to C++. If you’re going to look at the most common Windows or Linux Apis, you’ll notice that variables that change are always passed by pointers, since in C there were no references. They created these references to make the program safer and "beautiful" because, after all, pointers are one of the most "unpredictable" things about programming errors.
If you know Assembly, the difference of a pointer is that (analogously, not literally) when you call a mnemonic or perform a function, it performs MNM var
, and with a reference, MNM [var]
. This is to say that C++ mostly treats references as dereferenced pointers, pointers as references to a variable, and vice versa. They are similar, but different in practice, as the example of function. In practice, when you have a data string in memory, ask for a pointer. If you need a variable to alter in memory, ask for a reference. The cleaner a code is, the less likely it is to be wrong.
Example:
void f(int&&);
int main()
{
int var;
int& referencia = var;
referencia = 1; // o valor de var agora é 1
std::cout << (int)&referencia; //imprime o endereço em que var se encontra, dando o ponteiro para a variável
&referencia = (int*)0; // ERRO: imutável
f(1); //imprime 1, valor literal
f(referencia); // ERRO
}
void f(int&& var)
{
std::cout << var;
}
I still don’t understand what you meant by the first point. It’s like you don’t exist
int& var = var2; var = 1;
– Lucas Henrique
@Lucashenrique this you did is a change in the referenced value, not a reference rivend to another variable.
– pepper_chico
@Lucashenrique pointers can be
int *var
orconst int *var
orint * const var
orconst int * const var
. One thing is the pointed type being constant, another the variable itself.– pepper_chico
Ah. That’s really right. It’s just a little ambiguous. I think it would be better if you put "references in themselves, that is, the memory block to which it points [...]"
– Lucas Henrique
@Lucashenrique is precisely "the block of memory to which she points" that may or may not be const, she herself is not, is always const. It would be wrong.
– pepper_chico
Let’s go continue this discussion in chat.
– Lucas Henrique
Pepper, it wouldn’t be possible to put code examples for each of the dots?
– Carlos Cinelli
@Carloscinelli Yes, I just don’t have time now. It’s later.
– pepper_chico
Java did not use the term reference before C++?
– Pablo Almeida
@Pablo believes not, c++ exists before the existence of java, and "reference" actually gives name to an element of the syntax of c++, different from the use of this term in java for example. Nevertheless, it is worth reading the article I passed about, which proves that java actually has a kind of restricted pointer, these are usually treated as "reference" in a certain way erroneous.
– pepper_chico