Explore the C + + object model in depth->2.4 members ' initialization team

Source: Internet
Author: User

One, in the following cases, you must use the member initialization list:

1, when the initialization of a reference;

2, when the initialization of a const member;

3, when calling a base class constructor, and it has a set of parameters;

4, when calling a member class constructor, and it has a set of parameters.

In these four cases, the initialization should be in the initialization list, otherwise the efficiency is not high.

Second, another common question is: can I call a member function to set an initial value of a member?

The answer is yes, and here's a code conversion:

X::X (int  val)    : I (Xfoo (Val)), J (Val) {}// is actually converted as follows x::x (/*  */ int val) {    this,Xfoo (val);      = val;}

In this conversion, the this pointer has been constructed properly. However, it is best to put the function in the constructor body, because the members it uses may not be initialized yet.

Explore the C + + object model in depth->2.4 members ' initialization team

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.