【发布时间】:2012-01-01 05:03:29
【问题描述】:
我有一个自定义类,我希望它的行为类似于内置类型。
但是我注意到您可以在不提供初始值的情况下初始化该类的 const 变量。我的班级目前有一个空的默认构造函数。
这是 int 和我的类 foo 的比较:
int a; // Valid
int a = 1; // Valid
const int a = 1; // Valid
const int a; // Error
foo a; // Valid
foo a = 1; // Valid
const foo a = 1; // Valid
const foo a; // Should cause an error, but it compiles
如你所见,我需要阻止
const foo a;
来自编译。
C++ 大师有什么想法吗?
【问题讨论】:
标签: c++ initialization constants initializer default-constructor