views:

304

answers:

3

Hi,

I have a class

class foo {
public:
   foo();
   foo( int );
private:
   static const string s;
};

Where is the best place to initialize the string s in the source file?

+2  A: 

In a translation unit within the same namespace, usually at the top:

// foo.h
struct foo
{
    static const std::string s;
};

// foo.cpp
const std::string foo::s = "thingadongdong"; // this is where it lives

// bar.h
namespace baz
{
    struct bar
    {
        static const float f;
    };
}

// bar.cpp
namespace baz
{
    const float bar::f = 3.1415926535;
}
GMan
+3  A: 

Static members need to be initialized in a .cpp translation unit at file scope or in the appropriate namespace:

const string foo::s( "my foo");
Michael Burr
+5  A: 

Anywhere in one compilation unit (usually a .cpp file) would do:

foo.h

class foo {
    static const string s; // Can never be initialized here.
    static const char* cs; // Same with C strings.

    static const int i = 3; // Integral types can be initialized here (*)...
    static const int j; //     ... OR in cpp.
};

foo.cpp

#include "foo.h"
const string foo::s = "foo string";
const char* foo::cs = "foo C string";
// No definition for i. (*)
const int foo::j = 4;

(*) According to the standards you must define i outside of the class definition (like j is) if it is used in code other than just integral constant expressions. See David's comment below for details.

squelart
I have upvoted, but after reviewing the standard there is an error in your code: `i` must be *defined* in the cpp. §9.4.2/4 *If a static data member is of const integral or const enumeration type, its declaration in the class definition can specify a constant-initializer which shall be an integral constant expression (5.19). In that case, the member can appear in integral constant expressions. The member shall still be defined in a name- space scope if it is used in the program and the namespace scope definition shall not contain an initializer.*
David Rodríguez - dribeas
Based on your quote from the standards, it seems `i` would have to be be defined *only* if it was used somewhere else than in integral constant expressions, right? In this case you cannot say that there is an error because there is not enough context to be sure -- or stricly speaking the above example is correct if there is no other code.Now I do appreciate your comment (+1), I'm still learning things myself! So I'll try and clarify that point in the answer, please let me know if it's better...
squelart