CC1 无法在类定义中初始化,为什么?那么我将如何初始化它?请有人帮帮我

CC1 cannot be initialized in the class definition, why? then how will I initialize it? please someone help me

本文关键字:初始化 定义 为什么 CC1      更新时间:2023-10-16

CC1 无法在类定义中初始化,为什么? 那么我将如何初始化它?

 class X
    {
    public:
     X(){}
     const char CC1 = 25;       // Line1
     static const int SCI = 50; // Line2
    } ;
        int main()
    {
     X Obj1;
    } 

在构造函数成员初始化列表中初始化它:

class X
{
public:
    X() : CC1(25) {}  // <--- here
    const char CC1;       
    static const int SCI = 50; 
} ;

但是,如果它对所有实例都25,为什么不让它也static呢?浪费内存符合您的要求吗?