【发布时间】:2012-11-28 14:27:16
【问题描述】:
是否可以在class 和global const 中初始化class method?我想在我的class 中使用一种方法来设置const。
我的想法是:
/* a.h */
class A {
private:
const string cs;
public:
A();
~A();
bool cs(const string &host, ...)
};
/* a.cpp */
A::A(){
}
A::~A(){
}
bool cs(const string &host, ...) {
/* check some values */
cs = "Set Vaule"; //Doesnt work, get an compiler error
}
是否可以在method 中设置global const?
【问题讨论】:
-
如果你可以在初始化后更改它,它就不会是 const。
-
您想要完成的工作的大局是什么?为什么不直接使用访问器方法来限制访问?
-
在什么意义上
cs是一个全局? -
您对“全球”世界的使用似乎暴露了您的一些困惑。您确实知道您的班级
A的每个实例都有cs的不同副本,对吗?其次,应避免将函数命名为与变量相同的名称。