When you manage pointers, is the information duplicated or is there some way to create something like hyperlinks?
If you have two normal variables, it is clear, as you can see in the following example, that the information is duplicated:
int a = 10;
int b = a;
std::cout << a << ' ' << b << '\n';
a = 20;
std::cout << a << ' ' << b << '\n';
If the pointers did this same thing ... What would be the reason for their existence? A pointer is a normal variable that, instead of storing a result or a numerical value, does the same with memory addresses. If two pointers point to the same memory region, then both will access the same information. That is, the information is not going to be duplicated or anything like that ... the information is in a certain region of memory and it is the pointers that store the position of that information in order to access it.
An equivalent in the real world are phone numbers. When you share your mobile number with your friends you are not physically giving them your mobile (they can not make calls in your place or read your messages). The mobile number only and exclusively serves to locate you and all your friends can have a copy of your mobile number.
How could it be done to pass a reference to the city and not a copy of the city?
In C ++ there are two concepts that are similar but not the same:
As with the pointers, references are variables that store a memory location (as the name suggests, they serve to reference other variables). However, unlike the pointers, the references present a series of restrictions, which means that they can not be considered as pointers.
-
References must be initialized in the construction
int a = 5;
int & ref1; // Error, referencia no inicializada
int & ref2 = a; // ok
std::cout << a << ref2 << '\n';
a = 6
std::cout << a << ref2 << '\n';
-
References can not be reassigned
int a = 5, b = 7;
int & ref = a;
ref = b; // Esto es equivalente a 'a = b'
std::cout << a << b << ref << '\n';
ref = &b; // Error, ref no es un puntero
Of course, thanks to these restrictions, it also has a number of advantages:
-
References are used as normal variables, so the code is presented in a more natural and friendly way:
int a = 0;
int & ref = a; // No hay que usar el operador &
int * ptr = &a; // Con los punteros si
ref = 5;
std::cout << a << ref << *ptr << '\n'; // Tampoco hay que usar *
-
In the case of object references, access to member functions and variables is done with the operator .
instead of the operator ->
:
std :: string cad="abcde";
std :: string & ref = cad;
std :: string * ptr = &
std :: cout < < ref.count () < < ptr-> count () < < '\ n';
The references are suitable for situations in which we know that these restrictions are not going to be a problem ... for example, until a few years ago it was usual to use references to avoid copying the text strings:
void imprimieMensaje(std::string const& mensaje)
{
std::cout << mensaje;
}
With all this, and finally answering your question, to make a copy of the city it would be enough to use normal variables:
struct persona{
Ciudad ciudadOrigen;
};
While to use references, the most common is to use pointers:
struct persona{
Ciudad* ciudadOrigen;
};
Note that you could also use references ... but then you would find the restrictions mentioned above and that has its impact:
- You should know the city before creating objects of type
persona
.
- Once the city is assigned, you can not change it. You would have to delete the object of type
persona
and create a new one with the new city.