小编him*_*s15的帖子

为什么你不能在 C++ 中初始化类内的变量

我知道你不能在不使用构造函数的情况下直接在类中初始化成员变量(静态常量除外)。

但只是想知道这背后的原因是什么。下面是代码片段

如果有任何机构可以提供帮助

class a 
{

    int c=5;

// giving error error C2864: 'a::c' : only static const integral data members can be 

// initialized within a class

    int b;


public:
    a():c(1),b(2){}

    void h()
    {
        printf("%d,%d",c,b);
    }
};

int main()
{

    a l;

    l.h();

    getchar();
}
Run Code Online (Sandbox Code Playgroud)

c++

4
推荐指数
1
解决办法
2937
查看次数

标签 统计

c++ ×1