Google C + + Programming style Guide (iv): Smart pointers and other C + + features

Source: Internet
Author: User
Tags constant

1. For smart pointers, safety first, convenient second, as localized as possible (SCOPED_PTR); 2. The reference form participates in the const, otherwise the pointer parameter is used; 3. Use of function overloads to be clear and readable 4. The use of default function parameters (questionable) is prohibited in view of their misuse; 5. Variable-length arrays are prohibited; 6. Reasonable use of friends ...

Google's unique style

Google has many of its own techniques and features that make C + + code more robust, as well as the use of C + + that is different from elsewhere.

1. Intelligent pointer (smart pointers)

If you do need to use smart pointers, Scoped_ptr is perfectly capable. In very special situations, such as objects in an STL container, you should use only std::tr1::shared_ptr, and do not use auto_ptr in any case.

The "smart" pointer looks like a pointer, but it's actually an object with semantics attached. In Scoped_ptr, for example, when Scoped_ptr is destroyed, the object it points to is deleted. SHARED_PTR is also the case, and shared_ptr implements the reference count (reference-counting) so that the pointer is deleted only if the last object it points to is destroyed.

In general, we tend to design objects that belong to explicit code, and the most explicit object affiliation is to use the object directly as a field or local variable without using a pointer at all. The other extreme is that the reference count pointer does not belong to any object, so the design problem is that it is easy to cause a circular reference or other bizarre conditions that cause the object to be deleted, and that the atomic operation is slow every time a copy or assignment is done.

Although this is not recommended, there are times when reference counting pointers are the simplest and most effective solution.

It seems that Google's difference is to try to avoid the use of smart pointers: D, when used as much as possible localization, and, security first.

Other C + + features

1. Reference parameters (Reference Arguments)

Therefore, parameters passed by reference must be added with Const.

Definition: In the C language, if a function needs to modify the value of a variable, the formal parameter (parameter) must be a pointer, such as int foo (int *pval). In C + +, a function can also declare a reference parameter: int foo (int &val).

Advantages: Defining parameters as references avoids ugly code like (*pval) + +, which is required for applications like copy constructors, and does not accept null pointers as pointers.

Disadvantage: Misleading, because the reference is syntactically a value but has the semantics of the pointer.


In a function parameter list, all references must be const:

void Foo (const string &in, String *out);

In fact, this is a hard convention: the input parameter is a value or a constant reference, the output parameter is a pointer, and the input parameter can be a constant pointer, but cannot use a very few reference parameter.

You can use constant pointers when emphasizing that the parameter is not a copy, and you must always exist during the lifetime of the object, preferably in the comments. STL adapters such as bind2nd and Mem_fun do not accept reference parameters, and in this case the function must be declared with a pointer parameter.

Related Article

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: 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.