CC1 无法在类定义中初始化,为什么?那么我将如何初始化它?请有人帮帮我
CC1 cannot be initialized in the class definition, why? then how will I initialize it? please someone help me
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
呢?浪费内存符合您的要求吗?
相关文章:
- 标准是否使用多余的大括号(例如 T{{{10}}})定义列表初始化?
- Boost Python Numpy - 要初始化的未定义引用
- 虚拟成员函数的定义是否强制在同一转换单元中动态初始化静态数据成员?
- C++类中的类作为自定义向量(如何初始化?
- 初始化类定义中的结构数组
- C++ 如何检查 char 变量是否未定义(未初始化)
- C++列表初始化允许多个用户定义的转换
- (2 问题)"类"类型重新定义(即使 #pragma 一次),以及静态函数内的静态成员对象初始化?
- (C++)我的自定义数组无法初始化(编译错误)
- 类模板静态数据成员定义/声明/初始化
- 初始化在类类型 #define 中定义的非静态成员数组,不带默认 ctor
- 在另一个文件中初始化的 extern 数组中未定义的引用
- thread_local静态成员模板定义:初始化失败,GCC
- 通过用户定义的转换初始化引用
- 使用无效指针初始化指针声明符的行为是否未定义?
- 您可以在类初始化器列表中定义数组大小吗?
- 如何使用自定义比较器初始化类数据成员,该成员是 std::set
- 获取未初始化对象成员的地址是否定义良好?
- 初始化自定义类型构造函数的数组
- 使用用户定义的构造函数初始化C++结构