Dan*_*ker 18
你可以 - 你把const放在类型名称的前面.
class C
{
const int x;
public:
C() : x (5) { }
};
Run Code Online (Sandbox Code Playgroud)
小智 8
如果它不是成员,您可以声明它.请注意,将变量声明为const将对使用该类的方式产生相当大的影响.你肯定需要一个构造函数来初始化它:
class A {
public:
A( int x ) : cvar( x ) {}
private:
const int cvar;
};
Run Code Online (Sandbox Code Playgroud)