Is it possible to initialize in a class a global const in a class method? I would like to use a method in my class to set the const.
My idea was:
/* 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
}
Is It possible to set a global const in a method?
No. You could initialize it in a constructor initializer, but once initialized a
constmember cannot be changed. Otherwise, it wouldn’t be aconstant, now, would it?