The content obtained on the internet for viewing.

Source: Internet
Author: User

1. c ++Meaning of Symbol

My question is about initializing C ++ class members. I have seen a lot of such code (including in your topic ):
Csomeclass: csomeclass ()
{
X = 0;
Y = 1;
}
In other places, it is written as follows:
Csomeclass: csomeclass (): x (0), y (1)
{
}
Some of my programmers say the second method is better, but they don't know why. Can you tell me the differences between the two types of member initialization methods?

Answer

Technically, your programmers are right, but in most cases, there is no difference between the two. There are two reasons for us to choose the second syntax,
It is called the member initialization list. One reason is required, and the other is for efficiency.

Let's take a look at the first reason-necessity. Suppose you have a class member, which is a class or structure and has only one constructor with one parameter.

Class cmember {
Public:
Cmember (int x ){...}
};
Because cmember has an explicitly declared constructor, the compiler does not generate a default constructor (without parameters), so it cannot be created without an integer.
An instance of cmember.

Cmember * PM = new cmember; // Error !!
Cmember * PM = new cmember (2); // OK

If cmember is a member of another class, how do you initialize it? You must use the member initialization list.

Class cmyclass {
Cmember m_member;
Public:
Cmyclass ();
};

// You must use the member initialization list

Cmyclass: cmyclass (): m_member (2)
{
???
}

There is no other way to pass the parameter to m_member, if the member is a constant object or reference is the same. According to the C ++ rules, constant objects and references cannot be assigned values. They can only be initialized.

The second reason is that for efficiency consideration, when the member class has a default constructor and a value assignment operator. The cstring of MFC provides a perfect example.
Assume that you have a cmyclass member m_str of the cstring type, and you want to initialize it as "yada .". You have two options:

Cmyclass: cmyclass (){
// Use the value assignment operator
// Cstring: Operator = (lpctstr );
M_str = _ T ("yada ");
}

// Use the class member list
// And constructor cstring: cstring (lpctstr)
Cmyclass: cmyclass (): m_str (_ T ("yada "))
{
}

Are there any differences between them? Yes. The compiler always ensures that all member objects are initialized before the constructor is executed. Therefore, compile
The code will call cstring: cstring to initialize m_str, which is completed before the control reaches the value assignment statement. In the second example, the compiler generates a pair
Cstring: cstring (lpctstr) Call and pass "yada" to this function. The result is that two cstring functions are called in the first example.
(Constructor and value assignment operator). In the second example, only one function is called. In the cstring example, this does not matter because the default constructor is
Inline, cstring only allocates memory for the string as needed (that is, when you actually assign a value ). However, repeated function calls are a waste of resources,
Especially when constructors and value assignment operators allocate memory. In some large classes, you may have a constructor and a value assignment operator to call the same
An init function that allocates a large amount of memory space. In this case, you must use the initialization list to avoid unnecessary memory allocation twice. Internal types such
For ints, longs, or other types without constructors, there is no performance difference between the initialization list and the assignment in the constructor body. No matter which
One method only has one value assignment. Some programmers say that you should always use the initialization list to maintain good habits, but I have never found the two
What are the difficulties in conversions between methods. In programming style, I tend to use assignment in the subject, because there is more space for formatting and adding comments, you can write
Such statement: x = y = z = 0; or memset (this, 0, sizeof (this ));
Note that the second part is definitely not object-oriented.
When I consider the initialization list issue, there is a strange feature I should warn you that it is about C ++ initialization class members, they are beginning in the declared order
Instead of following the order in the initialization list.

Class cmyclass {
Cmyclass (int x, int y );
Int m_x;
Int m_y;
};

Cmyclass: cmyclass (int I): m_y (I), m_x (m_y)
{
}

You may think that the above Code will first do m_y = I, then m_x = m_y, and finally they have the same value. But the compiler first initializes m_x, then m_y, because
They are declared in this order. The result is m_x, which will have an unpredictable value. My example is designed to illustrate this point. However, this bug will occur more naturally.
There are two ways to avoid it. One is to declare members in the order you want them to be initialized, and the other is, if you decide to use the initialization list, always follow
The Declaration Order lists these members. This will help eliminate confusion.

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.