Google C + + Coding Style: Reference parameters

Source: Internet
Author: User

Google C + + Coding style definition

The input parameter is passed in as a value or a const reference, and the output parameter uses a pointer. All input parameters in the form of a reference must be const, which is the form of the const t&.

such as the following form:

void Foo(conststring &instring *out);

In the following cases, you can use the form of the const t*:
* The need for pointers to be empty (i.e. null pointers are justified).
* Requires the use of pointers or reference forms to input parameters.

Why use the const t& form?

Passing in a value is the safest form, because it always provides a copy to the function. For complex parameters (structs or classes), this also introduces the overhead of unnecessary copies. Use pointers and references to solve this problem. The reference is more secure and avoids unnecessary null pointer judgments. So the input parameter is defined in the form of const t&, similar to the semantics of the passed value, which avoids the copy and avoids accidental modification.

The following example shows that even if the function is defined as a const t& form, there is still the possibility of a null pointer being introduced:

Aa) {     a10;A* b = NULL; test(*b);

This is a bad example of using references, syntactically legal, but such implementations are not allowed. The duty to empty is the caller, not the function. (Refer to: How does the check for NULL when passing by reference in C + +?)

In addition, because the reference, in its nature is also provided as a pointer, so the performance relative to the value of a bit lower. So a simple data type is still going to pass the value.

Why is the output parameter a pointer?

In the form of references and pointers, they can be used as output parameters of a function. If there is no const-decorated reference, the parameter of the reference form can be an output parameter. This may cause the reader of the code to have an illusion about the type of the parameter. It is the Google C + + Coding style that refers to the syntax of a reference with a value class, but it is the semantics of the pointer. The reader of the code questions the value of being able to change a parameter, assuming that the value is passed in, but the values have changed. Although it is possible to look at the function declaration, if a large project, still want to achieve at a glance.

Summarize

As the code specification is defined, mainly in order to unify everyone's coding habits, reduce some "surprise". Simply from the value of the transmission, the reference, or the form of a pointer, cplusplus.com on the one can also be used as a reference when to pass the parameters by value, reference, and pointer. Summarize the core points of the parameter three incoming form:
1. The highest pass performance, but with the cost of copying.
2. It is not legal to pass the time-space value, not to be sentenced to empty, no copy of the cost.
3. The pointer time-space value is legal, it needs to be empty, and there is no copy overhead.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Google C + + Coding Style: Reference parameters

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.