基于Bearvine在这里的回答Static constructor in c++
我已经采用了以下C#代码:
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");
}
}
}
在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;
}
问题 - 这是在C ++中复制.NET设计模式的最佳方法吗?或者,在将代码迁移到C ++时,您会建议使用替代类设计吗?