[c + +] In-class initializer

Source: Internet
Author: User
Tags class definition

C++11 introduced serveral contructor-related enhancements including:

    • Class member initializers
    • Delegating controctors

This article discusses is about Class member initializers Only.

Class member initializers is also called In-class Initializers. C++11 follows other programming language so let's initializer a class member directly during its declaration:

// c++11    int 5;    // In-class initializer    bool flag (false);    // another in-class initializer  public :    M ();}; M m1;     // M1.J = 5, M1.flag = False

The complier transforms every member initializers (such as int j = 5) into a controctor ' s member Initializer. Therefore, the Declaration of class M above is semantically equalment to the following c++03 class Definition:

class m2{    int  j;     BOOL flag;  public :    M2 (): J (5), flag (false) {}}

If The constructor includes an explict member initializer for a member that also have an in-class initializer, the CONTROCT or ' s member Intializer would override the In-class Initializer.

class m2{    int7;    // In-class initializer  public :    M2 ();     // j = 7    M2 (int i): J (i) {}    //  overrides J ' s In-class intializer}; M2 m2;      // j = 7 M2 m3 (5);    // j = 5

Reference:

C++11 tutorial:new Constructor Features that make Object initialization Faster and smoother, smartbear.com

[c + +] In-class initializer

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.