C ++ initializes the member list

Source: Internet
Author: User

In the process of using C ++ programming, class members often need to be initialized. There are two common methods:
Method 1:
Cmyclass: csomeclass ()
{
X = 0;
Y = 1;
}
Method 2:
Csomeclass: csomeclass (): x (0), y (1)
{
}
This article will discuss the similarities and differences between the two methods and how to use them. Technically speaking, the second method is better, but in most cases, there is actually no difference between the two. The second syntax is called the member initialization list. There are two reasons for using this syntax: one is that this is required, and the other is for efficiency.

(1) If we have a class member, which is itself a class or a structure, and this member has only one constructor with parameters, there is no default constructor, to initialize this class member, you must call the parameter-containing constructor of this class member. If there is no initialization list, it will not be able to complete the first step and an error will be reported.
Class ABC
{
Public:
ABC (int x, int y, int Z );
PRIVATE:
Int;
Int B;
Int C;
};
Class myclass
{
Public:
Myclass (): ABC (1, 2, 3 ){}
PRIVATE:
ABC;
};

Because ABC has a displayed constructor with parameters, it cannot rely on the compiler to generate a constructor without parameters. Therefore, without three int-type data, ABC objects cannot be created.
The ABC class object is a member of myclass. to initialize this object ABC, you can only use the Member to initialize the list. There is no other way to pass the parameter to the ABC class constructor.
In another case, when a class member contains a const object or a reference, they must also initialize the object through the member initialization list, because these two objects need to be initialized immediately after Declaration, and in the constructor, they are assigned a value, which is not allowed.
(2) efficiency requirements:
The construction sequence of class objects is displayed. after entering the constructor body, the computation is performed to assign values to them. Obviously, the assignment and initialization are different, this shows the difference in efficiency. If you do not need to initialize the class table, the class performs an implicit default constructor call for its class members, and a copy operator call, if it is a class object, the efficiency will not be guaranteed.

Note: The data members to be initialized by the constructor will be initialized here, whether or not they are displayed in the constructor's member initialization list, in addition, the initialization sequence is consistent with the declared sequence and has nothing to do with the order of the list. Therefore, you must pay special attention to ensure that the two are consistent in order to truly ensure their efficiency.

 

Http://dev.firnow.com/course/3_program/c++/cppxl/2008105/147585.html ()

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.