Based on Bearvine's answer here Static constructor in c++
I've taken the following C# code:
namespace Services
{
internal static class Strings
{
private static Dictionary<uint, string> stringIDs = new Dictionary<uint, string>(0x2);
static Strings()
{
stringIDs.Add(0x1, "String1");
stringIDs.Add(0x2, "String2");
}
}
}
And in C++
#include <iostream>
#include <unordered_map>
#include <string>
namespace Service
{
class Strings
{
private:
static std::unordered_map<unsigned int,std::wstring> stringIDs;
public:
static void init (void);
};
void Strings::init()
{
stringIDs.insert({0x1, L"String1"});
stringIDs.insert({0x2, L"String2"});
}
}
std::unordered_map<unsigned int,std::wstring> Service::Strings::stringIDs;
int main()
{
Service::Strings::init();
// program etc
return 0;
}
Question - is this the best way to replicate this .NET design pattern in C++ ? Or would you recommend an alternate class design when migrating code to C++?