Static member initialization for classes in C + +

Source: Internet
Author: User
Tags function definition

Initialization of static members:

As with global objects, a static data member can only provide a definition in a program, which means that the initialization of a static data member should not be placed in the header file but should be placed in a file containing the class's non-inline function definition.

Transferred from: http://blog.csdn.net/veryday_code/article/details/7921833

There is only one member that can be initialized in a class, which is a static constant member.

class a{  private:    staticconstint0//  Static constant members can be initialized within a class };

Conclusion:

    1. A static constant data member can be initialized within a class ( that is, initialized at the same time as declared within a class), or it can be initialized in an implementation file outside of a class, which cannot be initialized in a constructor or initialized in a constructor's initialization list;
    2. Static and very data members can only be initialized in an implementation file outside the class, that is, in the constructor, and cannot be initialized in the initialization list of the constructor function;
    3. A non-static constant data member cannot be initialized within a class, nor can it be initialized in a constructor, but only and must be initialized in the constructor's initialization list;
    4. nonstatic non-static data members cannot be initialized within a class, can be initialized in constructors, or can be initialized in a constructor's initialization list;

Static member initialization for classes in C + +

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.