The Initialize list of C + + Class

Source: Internet
Author: User

Performance issues, it is necessary to initialize the list in some situations where the initialization list

    • Constant members, because constants can only be initialized and cannot be assigned, so they must be placed in the initialization list
    • Error1 (ConstChar* Constmsg):d ATA (msg)

      {

      data = msg;

      }

    • Reference type, the reference must be initialized at the time of definition and cannot be re-assigned, so it is also written in the initialization list
    • There is no default constructor for the class type, because using an initialization list can be initialized without calling the default constructor, but instead calling the copy constructor directly.
    • class Error2

      {

      char * data;

      Public :

      Error2 (ConstChar* Constmsg= 0):d ata (msg){}//error2 ()

      };

      class Error1

      {

      char * data;

      Error2 E2;

      Public :

      Error1 (ConstChar* Constmsg=0):d ata (msg)

      {

      e2 = E2out;

      data = msg;

      }

      Error1 (const char* Const MSG = 0):d ata (msg) {}

      Error1 (ConstChar* Constmsg, Error2&E2out):d ATA (msg), E2 (E2out)

      {

      e2 = E2out;

      data = msg;

      }

      };

      If not, there'll be a waring,
      • But for class types, it's a good idea to use an initialization list. As the above test shows, the process of invoking the default constructor one less time with the initialization list is very efficient for data-intensive classes. Also see the example above, we use the initialization list to implement the Test2 constructor
        struct test2{    Test1 test1;     &T1): test1 (t1) {}}
      • Using the same calling code, the output is as follows.

        The first line of output corresponds to the first line of the calling code. The second line outputs the initialization list of the corresponding Test2, invoking the copy constructor directly to initialize the Test1, eliminating the process of invoking the default constructor. So a good rule is to use the initialization list whenever possible using the initialization list.

The Initialize list of C + + Class

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.