C++;Java静态块的替代方案

C++;Java静态块的替代方案,java,c++,idioms,static-initialization,Java,C++,Idioms,Static Initialization,我正在编写一个date类,其中我需要一个静态映射,将“Jan”映射到1,以此类推。我想知道如何初始化静态映射。这就是我目前正在做的,但我只是觉得额外的if语句与Java中的静态块相比是不雅观的。我理解C++程序的编译要复杂得多,但我仍然想知道是否有更好的解决方案。 class date { static map<string, int> month_map; int month; int year; public: class input_format

我正在编写一个date类,其中我需要一个静态映射,将“Jan”映射到1,以此类推。我想知道如何初始化静态映射。这就是我目前正在做的,但我只是觉得额外的if语句与Java中的静态块相比是不雅观的。我理解C++程序的编译要复杂得多,但我仍然想知道是否有更好的解决方案。
class date {
    static map<string, int> month_map;
    int month;
    int year;
public:
    class input_format_exception {};
    date(const string&);
    bool operator< (const date&) const;
    string tostring() const;
};

map<string, int> date::month_map = map<string,int>();

date::date(const string& s) {
    static bool first = true;
    if (first)  {
        first = false;
        month_map["Jan"] = 1;
        month_map["Feb"] = 2;
        month_map["Mar"] = 3;
        month_map["Apr"] = 4;
        month_map["May"] = 5;
        month_map["Jun"] = 6;
        month_map["Jul"] = 7;
        month_map["Aug"] = 8;
        month_map["Sep"] = 9;
        month_map["Oct"] = 10;
        month_map["Nov"] = 11;
        month_map["Dec"] = 12;
    }   
    // the rest code.
}

// the rest code.
上课日期{
静态地图月图;
整月;
国际年;
公众:
类输入\格式\异常{};
日期(常量字符串&);
布尔运算符<(常数日期和)常数;
字符串tostring()常量;
};
映射日期::月\映射=映射();
日期::日期(常量字符串(&s){
静态布尔优先=真;
如果(第一){
第一个=假;
月地图[“一月”]=1;
月地图[“二月”]=2;
月地图[“三月”]=3;
月地图[“四月”]=4;
月地图[“五月”]=5;
月地图[“六月”]=6;
月地图[“七月”]=7;
月地图[“八月”]=8;
月地图[“九月”]=9;
月地图[“十月”]=10;
月地图[“11月”]=11;
月地图[“十二月”]=12;
}   
//其余代码。
}
//其余代码。

在C++11中,可以使用初始值设定项列表:

map<string, int> date::month_map = { {"Jan", 1},
                                     {"Feb", 2}
                                     // and so on
                                   };
map date::month_map={{“Jan”,1},
{“Feb”,2}
//等等
};

在C++03中,我相信您一直在做当前正在做的事情。

对于非C++11系统:使用helper函数并使
month\u map
成为
date
的静态常量成员怎么样,因为看起来您永远都不想更改月份名称与编号的关联,是吗?通过这种方式,
month\u map
在cpp文件中初始化,而不是在构造函数中初始化,因为它会把事情搞砸。(也许将来您将有几个构造函数,然后您将不得不编写大量样板代码)

const std::map createMonthMap()
{
映射结果;
//做一些事情
返回结果;
}
const std::map date::month_map(createMonthMap());
<代码> > p>你可以在C++中实现静态块特征,甚至是C++ 11。请看我的详细回答;它会让你做的很简单

#include "static_block.hpp"

static_block {
    month_map["Jan"] = 1;
    month_map["Feb"] = 2;
    month_map["Mar"] = 3;
    month_map["Apr"] = 4;
    month_map["May"] = 5;
    month_map["Jun"] = 6;
    month_map["Jul"] = 7;
    month_map["Aug"] = 8;
    month_map["Sep"] = 9;
    month_map["Oct"] = 10;
    month_map["Nov"] = 11;
    month_map["Dec"] = 12;
}   

但是,使用初始值设定项列表要好得多,因此如果您有C++11编译器,请务必使用@syam's suggest之类的列表。

这里有一个相关的问题:这可能有助于您了解您的选项。也给出了额外的提示。实际上,OP并没有被他/她目前正在做的事情所困扰,这似乎是我的答案。除了那句话,我想投你一票。。。
#include "static_block.hpp"

static_block {
    month_map["Jan"] = 1;
    month_map["Feb"] = 2;
    month_map["Mar"] = 3;
    month_map["Apr"] = 4;
    month_map["May"] = 5;
    month_map["Jun"] = 6;
    month_map["Jul"] = 7;
    month_map["Aug"] = 8;
    month_map["Sep"] = 9;
    month_map["Oct"] = 10;
    month_map["Nov"] = 11;
    month_map["Dec"] = 12;
}