Declaration and initialization of general variables, const, static, static const (const static) member variables in a class in C + +

Source: Internet
Author: User

C + + classes have several types of data members: normal type, constant (const), static (static), and static Const. Here are the following ways of initializing them before and after c++11 respectively.

Initialization of previous versions of C++11

Before c++11, regular data member variables can only be initialized in constructors and initialization lists. A member variable of the const type can only be initialized in the list and must be initialized here. The static type can only be initialized outside of the class. The static const type can be initialized within the class in addition to the integer number, and others can only be initialized outside the class. The code is as follows:

classa{ Public: A (); ~a ();//int m_n1 = 10; Error 1 C2864: "A::M_N1": only static const integer data members can be initialized in class    intM_N2;intM_n3;//const int m_nconst_1 = 10; Error 3 C2864: "a::m_nconst_1": only static const integer data members can be initialized in class    Const intm_nconst_2;Const intM_nconst_3;Const intM_nconst_4;//static int m_nstatic_1 = 10;//Error 1 C2864: "a::m_nstatic_1": only static constant integer data members can be initialized in class    Static intm_nstatic_2;Static intM_nstatic_3;//static double m_dstatic_1 = 10.1;//static data member with initialization expression in class, must have immutable constant shaping type, or must be specified as "inline"    Static Doublem_dstatic_2;Static DoubleM_dstatic_3;Static Const intM_nstaticconst_1 =Ten;Static Const intm_nstaticconst_2;Const Static intM_nstaticconst_3;//static Const Double m_dstaticconst_1 = 10; 1 Error C2864: "a::m_dstaticconst_1": only static constant integer data members can be initialized in the class     Static Const Doublem_dstaticconst_2;Static Const DoubleM_dstaticconst_3;};

The source files are as follows:

#include "Initialize.h"intA::m_nstatic_3 = -;//Initialize static variables outside of class//int a::m_nconst_4 = 13; Non-static data members cannot be defined outside of their classConst intA::m_nstaticconst_3 = A;//out-of-class initialization static constantsA::a (): M_n3 ( A), M_nconst_2 ( One), M_nconst_3 ( A), M_nconst_4 ( -)//,m_dstaticconst_2 (11.0)//, M_nstaticconst_3 (a)//M_nstatic_3 (13)//Not a non-static data member or base class for class "a"{Errors 4 Error C2758: "a::m_nconst_1": Must be initialized in constructor base/member initializer listM_N2 = One;//m_nconst_3 = 12; Expression must be a modifiable left -hand value    //m_nstatic_2 = 12; Cannot parse external symbol    //m_dstatic_2 = 11.0; Cannot parse external symbol    //m_nstaticconst_2 = 11; Expression must be a modifiable left -hand value    //m_dstaticconst_2 = 11.0; Expression must be a modifiable left -hand value}a::~a () {}

As the following table:
| Type |normal | Const | static | static Const |
| -- | -- | -- | -- | --|
| Initialize at declaration Time | x | x | x | X (only static constant integer type is possible) |
| Initialize List initialization | √| √| x | x |
| Constructor Initialization | √| x | x | x |
| Out-of-class initialization | x | x | √| √|

Initialization in C++11

In-class initialization is included in c++11, and regular data member variables can be initialized within the class, in constructors, and in the initialization list. A member variable of the const type can only be initialized in the list and must be initialized here. The static type can only be initialized outside of the class. The static const type can be initialized within the class in addition to the integer number, and others can only be initialized outside the class. The code is as follows:

classa{ Public: A (); ~a ();intM_N1 =Ten;intM_N2;intM_n3;DoubleM_D1 =10.0;Const intM_nconst_1 =Ten;Const intm_nconst_2;Const intM_nconst_3;Const intM_nconst_4;//static int m_nstatic_1 = 10;//static data member with initialization expression in class, must have immutable constant shaping type, or must be specified as "inline"    Static intm_nstatic_2;Static intM_nstatic_3;//static double m_dstatic_1 = 10.1;//static data member with initialization expression in class, must have immutable constant shaping type, or must be specified as "inline"    Static Doublem_dstatic_2;Static DoubleM_dstatic_3;Static Const intM_nstaticconst_1 =Ten;Static Const intm_nstaticconst_2;Static Const intM_nstaticconst_3;//static const Double m_dstaticconst_1 = ten;//E1591 a member of type "Const Double" cannot contain an in-class initializer    Static Const Doublem_dstaticconst_2;Static Const DoubleM_dstaticconst_3;};

The source file code is as follows:

int13;// int A::m_nConst_4 = 13; 非静态数据成员不能在其类的外部定义constdouble12.0;A::A():m_n3(12),m_nConst_2(11),m_nConst_3(12),m_nConst_4(13)//, m_nStaticConst_3(12)// m_nStatic_3(13)// 不是类"A"的非静态数据成员或基类{    11;    // m_nConst_3 = 12;          // 表达式必须是可修改的左值    // m_nStatic_2 = 12;         // 无法解析外部符号    // m_dStatic_2 = 11.0;       // 无法解析外部符号    // m_nStaticConst_2 = 11;    // 表达式必须是可修改的左值    // m_dStaticConst_2 = 11.0;  // 表达式必须是可修改的左值}A::~A(){}

As the following table:

type Normal Const Static Static const
Initialize at declaration time X X X (only static constant integer type is possible)
Initialization list Initialization X X
Constructor in the initialization function. X X X
Out-of-class initialization X X

Declaration and initialization of general variables, const, static, static const (const static) member variables in a class 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.