-
Notifications
You must be signed in to change notification settings - Fork 0
Referencias
Es posible crear referencias a cualquier tipo. Se crean como cualquier otra variable, con la salvedad de que, entre el nombre del tipo, y el nombre de la variable, se insert un ampersand('&'). Las referencias se crean siempre igualándose a una variable (o a otra referencia), y una vez creadas, es como si se utilizase la variable misma. Así, se podría decir que se está creando un nuevo nombre para la misma variable. También, desde un punto de vista más cercano a la implementación, se puede asumir que se trata de un puntero, pero teniendo en cuenta que a) no es necesario utilizar el operador '*' para acceder al valor apuntado, y b) una referencia nunca cambia la variable a la que apunta.
You can create references to any type. They are created like any other variable, except that between the type and the name, you must include an ampersand ('&'). References are always created assigning them to another variable (or another reference), and once created, is as the very same target variable was being used. A simple way to understand references is that it is just another name for the target variable. Also, it is possible to consider, from an implementation point of view, that it is a pointer, but taking into account that: a) you don't need to use the '*' operator in order to access the value of the pointed variable, and b) a reference can never change the pointed variable.
char c = 'a'
> 0x4: 'a'
char &refc = c
> 0x4: 'a'
printf(refc)
> 'a'
refc = c + 1
> 'b'
printf(c)
> 'b'
int i = 42
> 0x4: 42
int &refi = i
> 0x4: 42
printf(refi)
> 42
refi = i + 1
> 43
printf(i)
> 43
double d = 42.0
> 0x4: 42.0
double &refd = d
> 0x4: 42.0
printf(refd)
> 42.0
refd = i + 1
> 43.0
printf(d)
> 43.0
int i = 42
> 0x4: 42
int &refi = i
> 0x4: 42
printf(refi)
> 42
int &refi2 = refi
> 0x4: 42
printf(refi2)
> 42
refi = refi2 + 1
> 43
printf(i)
> 43