Same:
Pointers and references look completely different, but they seem to have the same functionality, and they all indirectly refer to other objects.
Difference:
1): references must always point to certain objects, while pointers can not point to any object. ( If the variable definitely points to an object, such as your design does not allow the variable to be empty, then you can declare the variable as a reference )
2): The reference must be initialized, and the pointer can not be initialized. For example, the following example
String &str; //error, reference must be initialized
String S ("ABCD");
string &str = S; //correct, str pointing to S
String *ps; //Legal, but dangerous.
3): The reference cannot be re-assigned again to point to another object after initialization of the assigned object, and the pointer can. For example, the following example:
String S1 ("Jimmy");
String S2 ("star");
string &rs = S1; //RS reference S1
String *ps = &s1; //PS point to S1
rs = s2; //RS still references S1, but the value of S1 is now "star"
PS = &s2; //PS now pointing to S2,S1 has not changed.
Efficiently, a reference is more efficient than a pointer, because it is not necessary to test its legitimacy before using a reference.
In general, you should use pointers when you consider the possibility of not pointing to any object (in which case you can set the pointer to null), and you need to be able to point to different objects at different times (in which case you can change the pointer's direction). If you always point to an object and do not change the point once it points to an object, then you should use a reference.
another case is that when you reload an operator, you should use a reference.
Recommendation:
you should not use pointers when you know that you must point to an object and do not want to change its point, or if you overload the operator and are misunderstood to prevent unnecessary semantics. In other cases, you should use pointers.
This article is from the "Jimmy http://jimmystar.blog.51cto.com/3716199/1584042 C + +" blog, make sure to keep this source.
The difference between a pointer and a reference in a bad complement C + +