The constructor initializer in C ++

Source: Internet
Author: User

 

 

Class
{
Public:
A (): A ({1, 2 })
{
};
PRIVATE:
Const int A [2];
};

Compilation failed! Thank you for your explanation.

Explanation:

1. Any member variables in the class cannot be initialized during definition.
2. Generally, data members can be initialized in the constructor.
3. the const data member must be initialized in the const initialization list.
4. Static should be initialized outside the class definition.
5. array members cannot be initialized in the initialization list.
Cannot specify explicit initializer for Arrays
You cannot specify an Explicit initialization for the array.
Enum {I = 10, A = 100}; can meet the requirements, and the const data member is a constant only within the object, and it is variable for the entire class!

 

Referrence:The constructor initializer

The constructor initializer starts with a colon, which is followed by a comma-separated list of data members each of which is followed by an initializer inside parentheses.

The constructor initializer is specified only on the constructor definition, not its declaration.

Data members of class type are always initialized in the initialization phase, regardless of whether the Member is initialized explicitly in the constructor initializer list. Initialization happens before the computation phase begins.

Constructor initializers are sometimes required

Some members must be initialized in the constructor initializer. for such members, assigning to them in the constructor body doesn' t work. members of a class type that do not have a default constructor and members that areConstOr reference types must be initialized in the constructor initializer regardless of type.

Advice: Use constructor initializers

In each classes, the distinction between initialization and assignment is strictly a matter of low-level efficiency: a data member is initialized and assigned when it cocould have been initialized directly. more important than the efficiency issue is the fact that some data members must be initialized.

We must use an initializer for anyConstOr reference member or for any member of a class type that does not have a default constructor.

By routinely using constructor initializers, we can avoid being surprised by compile-time errors when we have a class with a member that requires a constructor initializer.

Order of member Initialization

The order of initialization often doesn't matter. However, if one member is initialized in terms of another, then the order in which members are initialized is crucially important.

It is a good idea to write constructor initializers in the same order as the members are declared. Moreover, when possible, avoid using members to initialize other

Initializers may be any expression

An initializer may be an arbitrarily complex expression.

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.