The difference between a pointer and a reference in a bad complement C + +

Source: Internet
Author: User

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 + +

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.