I have a class that I would like to store a static std::string
that is either truly const or effectively const via a getter.
I've tried a couple direct approaches
1.
const static std::string foo = "bar";
2.
const extern std::string foo; //defined at the bottom of the header like so
...//remaining code in header
}; //close header class declaration
std::string MyClass::foo = "bar"
/#endif // MYCLASS_H
I've also tried
3.
protected:
static std::string foo;
public:
static std::string getFoo() { return foo; }
These approaches fail for these reasons respectively:
- error: in-class initialization of static data member
const string MyClass::foo
of non-literal type - storage class specified for
foo
-- it doesn't seem to like combiningextern
withconst
orstatic
- many 'undefined reference to' errors generated by other parts of my code and even the return line of the getter function
The reason I would like to have the declaration within the header rather than source file. This is a class that will be extended and all its other functions are pure virtual so I currently have no other reason than these variables to have a source file.
So how can this be done?
See Question&Answers more detail:os