我使用了一个成员变量,并且在程序的某个点上我想要更改它,但我更喜欢在其他地方“锁定它”,以防止意外的更改。
用于解释的代码:
class myClass {
int x; // This should be prevented to being changed most of the time
int y; // Regular variable
myclass() {x = 1;}
void foo1 () {x++; y++;} // This can change x
void foo2 () {x--; y--;} // This shouldn't be able to change x
// I want it to throw a compile error
};问题是:它能以某种方式实现吗?像permanent const_cast这样的?
我知道我可以立即使用构造函数初始化列表和常量,但我需要稍后更改我的变量。
发布于 2012-12-14 02:56:25
你不能做你想做的事...如果我错了,谁来纠正我。
https://stackoverflow.com/questions/13866440
复制相似问题