Google C + + Programming style Guide (iii): C + + Class

Source: Internet
Author: User
Tags constructor

About the class considerations, summed up: 1. Do not do too much logic-related initialization in the constructor; 2. The default constructor provided by the compiler does not initialize the variable, and if other constructors are defined, the compiler is no longer available, requiring the coder to provide the default constructor; 3. To avoid implicit conversions, you declare a single argument constructor as explicit ....

Class

Class is the basic unit of code in C + +, which is widely used. This section lists what to do and what not to do when writing a class.

1. Functions of constructors (constructor)

The constructor only makes those meaningless (trivial, translator note: Simple initialization has no practical logical meaning for program execution because the "meaningful" value of a member variable is mostly not determined in the constructor) initialization, if possible, using init () The method set is initialized to meaningful (non-trivial) data.

Definition: Performs an initialization operation in a constructor.

Advantages: Easy layout, no need to worry about whether class initialization.

Disadvantages: The problems caused by performing operations in constructors are:

1 The constructor is not easy to report an error and cannot use an exception.

2 The failure of the operation causes the initialization of the object to fail, resulting in an indeterminate state.

3 The constructor calls the virtual function, the call is not distributed to the subclass implementation, even if there is no subclass implementation, the future is still hidden.

4 If someone creates a global variable of that type (although violating the rules mentioned in the previous section), the constructor will be invoked before main (), potentially damaging the hypothetical conditions implied in the constructor. For example, GFlags has not been initialized.

Conclusion: If the object requires meaningful (non-trivial) initialization, consider using another init () method and/or add a member tag to indicate whether the object has been initialized successfully.

2. Default constructor (constructors)

If a class defines several member variables and no other constructors, you need to define a default constructor, or the compiler will automatically produce a default constructor.

Definition: When a new object with no parameters is created, the default constructor is invoked, and when new[is called (an array), the default constructor is always invoked.

Advantages: The default is to initialize the structure to an "impossible" value, making debugging easier.

Cons: This is redundant work for code writers.

Conclusion:

If a member variable is defined in a class and no other constructor is provided, you need to define a default constructor (without parameters). The default constructor is more appropriate for initializing objects, so that the internal state of the object (internal states) is consistent and valid.

The reason for providing a default constructor is that if you do not provide another constructor and you do not define a default constructor, the compiler will automatically generate one for you, and the compiler-generated constructor does not initialize the object.

If you define a class that inherits an existing class and you do not add new member variables, you do not need to define a default constructor for the new class.

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.