Skip to main content
gamedev.net
🔒 Locked

Initializing Static Const in Class Problem

Started by Raeldor Oct 19, 2005 at 1:11 PM 8 replies 850+ views
Original Post
Raeldor
Raeldor
I am trying to initialize a static const member in my class as follows...

		const static EventType Type("evt_moveactor");

But my compiler is giving my a syntax error:'string' error. I thought it was valid to initialize a static const member inside a class? Thanks
Sneftel
Sneftel
C++? C#? Java?
Raeldor
Raeldor
Quote:
Original post by Sneftel
C++? C#? Java?


sorry, C++.
stylin
stylin
#include <iostream>class foo {public:   static const int i = 420;   // initialize static const in-class   static const int j;};// const foo::i;               // not necessaryconst foo::j = 69;             // initialize static const out-classint main() {   std::cout << foo::i << std::endl;   std::cout << foo::j << std::endl;   return 0;}
:stylin: "Make games, not war." "...if you're doing this to learn then just study a modern C++ compiler's implementation." -snk_kid
Raeldor
Raeldor
Works for integers for me too, but no strings.
stylin
stylin
Quote:
Original post by Raeldor
Works for integers for me too, but no strings.

Only const static integral data members can be initialized within a class or struct.
:stylin: "Make games, not war." "...if you're doing this to learn then just study a modern C++ compiler's implementation." -snk_kid
Sneftel
Sneftel
Yep. Integers are specifically defined as being initializable directly in the class definition. All other datatypes need to be initialized when they're defined in the source file.
gumpy
gumpy
class foo{	static const std::string bar;};const std::string foo::bar( "foobar!" );


edit: boy am i slow and distracted today...
This space for rent.
Raeldor
Raeldor
Quote:
Original post by gumpy macdrunken
class foo{	static const std::string bar;};const std::string foo::bar( "foobar!" );


edit: boy am i slow and distracted today...


I see, thanks. And that initialization can sit in the header file even if the header file is included in multiple modules? Does this mean it can get initialized more than once when each module is loaded?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.