References in C++ are actually just restricted pointers, but there are semantic restrictions on the restricted pointer that cannot be used to perform some operations that pointers can do: reassignment, copy, ++, -- (I haven’t thought of others yet. ), and restricts the null value to improve security. It can be understood as a protection mode of the compiler for pointers.
So, consider when should you use references and when should you use pointers? I think there are a few points:
Citation first
Use references wherever references can be used instead of pointers.
Pointers are allowed to be null, but references are not.
const stock* top(...)
{
return NULL;
}
Of course, in this case you can still stick to using references, and in the case of empty objects, you can express it by throwing exception:
Of course, there are exceptions. If the associated _car is required to be variable during the lifetime of the trip, then you can only use pointers (because references cannot be reassigned):
Otherwise, if you insist on using references and want to change the pointer of _car, you can only reconstruct a new trip.
References cannot be saved in containers, only pointers can be used (I guess it is mainly because references are restricted in reassignment and copy operations):
std::vector<car> cars; // OK
std::vector<car*> cars; // OK
std::vector<car&> cars; // ERROR
Finally, you can convert the reference into a pointer at any time:
class maintainer
{
public:
void maintain(car& car)
{
car* p = &car;
// play with pointer 'p'
}
};
But you must pay attention to the situation. Here car is only passed in for use by the maintain method (the reference implies use relationship). Although the pointer operation is not elegant, it is still acceptable, but you must pay attention to saving the pointer for use:
class maintainer
{
car* _last_car;
public:
void maintain(car& car)
{
car* _last_car = &car;
// ...
}
void review_last_maintain()
{
// play with "_last_car"
}
};
After exiting the maintain method, the object pointed to by _last_car is likely to be released.
References in C++ are actually just restricted pointers, but there are semantic restrictions on the restricted pointer that cannot be used to perform some operations that pointers can do: reassignment, copy, ++, -- (I haven’t thought of others yet. ), and restricts the null value to improve security. It can be understood as a protection mode of the compiler for pointers.
So, consider when should you use references and when should you use pointers? I think there are a few points:
Citation first
Pointers are allowed to be null, but references are not.
Of course, in this case you can still stick to using references, and in the case of empty objects, you can express it by throwing exception:
Reasonable use semantics implies, pointers imply possession (has), references imply association (use):
car
has awheel
trip
use acar
:Of course, there are exceptions. If the associated _car is required to be variable during the lifetime of the trip, then you can only use pointers (because references cannot be reassigned):
Otherwise, if you insist on using references and want to change the pointer of _car, you can only reconstruct a new trip.
References cannot be saved in containers, only pointers can be used (I guess it is mainly because references are restricted in reassignment and copy operations):
Finally, you can convert the reference into a pointer at any time:
But you must pay attention to the situation. Here car is only passed in for use by the maintain method (the reference implies use relationship). Although the pointer operation is not elegant, it is still acceptable, but you must pay attention to saving the pointer for use:
After exiting the maintain method, the object pointed to by _last_car is likely to be released.
const is like a bottomless pit
Let’s give an example
Return NULL is allowed for pointers, but not for references.