class Interface {
public:
static const int i = 1;
static const double d = 1.0;
//! static const string *name = new string("Interface name");
virtual string getName() = 0;
}
Since C++ is a traditional truely compiled programming language,it could be easily convinced that it does allow object initialization(?).But why do C++ prohibit double initialization at the point of defintion?I see that g++ now support double initialization at the point of definition,but not msvc.
My question is,since it's easy to support primitive types – float/double initialization at the point of definition and it could make C++ programmer's life easier and happier with this convenient,why do C++ prohibit it?
P.S:
Reference – 9.2.4 section of C++ standard 2003.
A member-declarator can contain a
constant-initializer only if it
declares a static member (9.4) of
const integral or const enumeration
type, see 9.4.2.
Best Answer
Because otherwise there would be a question of which compilation unit (e.g. object file) the value lived in. Every file that included a header with a class definition would try to create an object that would be assigned to the static value on creation, potentially causing unpredictable behavior.
It's not just assignment that doesn't work; you also still need to define the static value outside of the class declaration. e.g.
I don't know if that's a good reason, but I suspect that's the logic behind it, anyway.