Google C + + Style Guide Read Note 4

Source: Internet
Author: User

Work in the class constructor
    • In general, constructors only set initial values for member variables, and complex initialization uses init ().
      • Signal error is difficult to handle, exceptions is forbidden.
      • If the initialization fails, the object creation fails, causing an unknown state.
      • If you call a virtual function in a constructor, these calls cannot be passed to the implementation of the child function. Even if there are no sub-functions, it is a hidden danger to future optimizations.
      • If a global variable is called, the construct will be preceded by Main (), which may cause implicit assumptions in the constructor to fail, such as a global variable initialization failure.
    • Summary: If your object does not handle complex initialization, explicitly use init (), do not call virtual functions in the constructor, do not throw an error, do not access uninitialized global variables.
Default constructor
    • You must define a default constructor, and if a member variable is defined in a class and there are no other constructors, the compiler will do the work for you, but it may not be good enough.
    • The constructor is always called when the new[] build vector is called.
    • Initialize the structure, hold the "impossible" value, and make debugging simple.
    • If your class is inherited and no new member variables are added, the default constructor is not required.
An explicit constructor
    • Use eplicit to avoid automatic type conversions that do not conform to requirements.
    • We require that the constructor of a single variable must be explicit.
Copy constructor
    • Use only when needed, otherwise turn off the function with Disallow_copy_and_assign.
    • When you create a copy of the object, the copy constructor and assignment operations are performed. For example, passing objects by value.
    • It is enough to use pointers or references in most cases.
    • You can create a copyfrom () or Clone () method.
      ' #define DISALLOW_COPY_AND_ASSIGN (TypeName) \
      TypeName (const typename&); \
      void operator= (const typename&)
      Class Foo {
      Public
      Foo (int f);
      ~foo ();
      Private
      Disallow_copy_and_assign (Foo);
      };
Structs and Classes
    • Only objects that store data use structs, and classes are used in other cases.
Inherited
    • Subclasses cannot overload non-virtual functions.
    • Classes with a "is-a" relationship can be used for inheritance.
    • If the class contains virtual functions, then the destructor is also written as a virtual function.
Interface
    • A class with the following conditions is a pure interface:
      • Only public pure virtual methods and static methods.
      • No non-static data members.
      • No constructors are defined, and if a constructor is provided, he must have no parameters and is protected.
      • If it is a subclass, it must inherit from the class with these conditions and add the interface prefix.
Operator overloading
    • Do not overload operators in special cases.
    • In order for some template functions to work, you may need to overload the operators.
    • Replace the overloaded operation with equal (), CopyFrom (), and avoid using the unary operator operator& if a predecessor definition is required.
Declaration Order
    • Public in front of private, method in front of the variable.
    • Public, prtected->private, each part in the following order:
      • Typedef and enumerations
      • Constant (static const data member)
      • constructor function
      • Destructors
      • Method that contains the static method
      • Data members
    • Friends are declared in private, disallow_copy_and_assign at the end of private.
Write short function
    • Functions over 40 rows should be considered if they can be disassembled.

Google C + + Style Guide Read Note 4

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