views:

296

answers:

2
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.

+1  A: 

I don't think you get a meaningful answer here. It's just happened so. And the new C++0x standard removes this limitation, which is the sign that there's no proper reason why.

Actually, this limitation is also inherited from C — you can't initialize structure members like that either.

Edit: now there is a hint in your example that suggests you're talking about static members. In your particular example you assign pointer to string, to the string. Other than that I will vote for scotchi answer now. There is, definitely, a logic behind it.

Michael Krelin - hacker
The limitation is hardly inherited from C. C doesn't support static struct members at all, much less allowing you to initialize them.
Adisak
Adisak, somehow I didn't read in the question that it's about static members.
Michael Krelin - hacker
@Michael:I have corrected the mistake.
Jichao
+2  A: 

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.

class Foo
{
  static std::string s;
};

std::string Foo::s = "foo";

I don't know if that's a good reason, but I suspect that's the logic behind it, anyway.

scotchi
Stroustrup explains the reasoning behind this "inconvenient restriction" (he wanted to avoid complicated linker rules) at http://www2.research.att.com/~bs/bs_faq2.html#in-class
Nate Kohl
this is correct. Its because you can include the header file many times. The linker will be confused where the variable actually lives.I dont know how c++0x is going to solve this.
Andrew Keith
I'm not a standards freak, but I'm not sure if c++0x deals with this. For what I know it allows specifying non-static members initializing in class declaration. And this is more or less a matter of syntax.
Michael Krelin - hacker
AFAIK, the difference is that C++0x is going to specify floating-point values to be handled in the same way that integer constant expressions already are. That's what already allows integer types to be the special exception to the rule about in-class initialization. If a compiler knows enough about how floating-point types behave on the target, to treat them as proper compile-time constants, then it's doable. But for whatever reason C++03 doesn't require that.
Steve Jessop
Programmer carry the Compiler Writer's burden?
Jichao