I have a small problem relating to namespaces, considering the following in a header file:
namespace A {
namespace B {
void SetMemberValue(double value) { _member = value; }
double FunctionThatUsesMember(double a) { return a * _member; }
double AnotherFuncThatUsesMember(double a) { return a / _member; }
static double _member = 0.01;
}
}
I did not want the user to be able to change the value of _member
via A::B::_member = some_value
. After reading up on unnamed namespaces I changed it to:
namespace A {
namespace B {
void SetMemberValue(double value) { _member = value; }
double FunctionThatUsesMember(double a) { return a * _member; }
double AnotherFuncThatUsesMember(double a) { return a / _member; }
namespace {
double _member = 0.01;
}
}
}
This forces the user to use the supplied mutator function and works great except for one problem:
If the user continues to use: A::B::_member = some_value
the code does not fail to compile, link, or run; the statement is simply ignored and the default value of 0.01
is used possibly leading to run-time errors or "OMG WTF IS WRONG BBQ!!1!!" moments. (The statement not failing may be an issue with MSVC++ and VS2010 though I am not sure.)
Question: Is there a way to have the code fail LOUDLY in some way when A::B::_member = some_value
is erroneously used?