When you define a static member of a class, regardless of whether or not that class is instantiated, that member is accessible through memory. In this case, since they're public members, using the strings themselves as keys is perfectly valid.
That said, your static constant members need to be initialized after the class definition, typically something like:
class Foo { static const int foo; };
const int Foo::foo = 42;
For your static map example, you need to keep in mind that the map must be initialized similarly if used as a class member. Here's a working example:
// Compiled with g++ --std=c++17 -Wall -Wextra -Werror ConstStaticMapExample.cpp -o ConstStaticMapExample
#include <iostream>
#include <string>
#include <map>
class A
{
public:
static const std::string NAME;
//Other class specific methods
};
const std::string A::NAME = "foo";
class B
{
public:
static const std::string NAME;
//Other class specific methods
};
const std::string B::NAME = "bar";
class C
{
public:
static const std::map<std::string, std::string> versionMap;
// More definitions
};
const std::map<std::string, std::string> C::versionMap = {{A::NAME,"aA"},{B::NAME,"bB"}}; // Reversed for explanation
int main(int,char**)
{
// local static
static const std::map<std::string, std::string> versionMap = {{A::NAME,"Aa"},{B::NAME,"Bb"}};
std::cout << "LOCAL STATIC EXAMPLE:" << std::endl;
for(auto mpair : versionMap)
{
std::cout << "Key: " << mpair.first << "\tVal: " << mpair.second << std::endl;
}
// class member static
std::cout << "CLASS MEMBER STATIC EXAMPLE:" << std::endl;
for(auto mpair : C::versionMap)
{
std::cout << "Key: " << mpair.first << "\tVal: " << mpair.second << std::endl;
}
return 0;
}
Gist