在C ++中替代静态

时间:2014-01-02 18:58:57

标签: c++ list map static

有没有办法在c ++中获取地图或列表,以保持旧条目的多次调用而不被定义为静态? 我正在使用多地图,问题是如果它是静态的,它只返回第一个条目,否则我最终得到一个空地图。

here's the code :

    typedef struct mystruct_1 {
    std::string         _name;
    int             _nb_ev;
    int             _nb_oc;
    std::list<mystruct_2>   _ev;
    mystruct_1() {}
} mystruct_1_t;



typedef struct mystruct_2 {
    int    _id;
    int    _nbs;
    char **_t;
    mystruct_2() {}
} mystruct_2_t;


myclass::method_1(){
static std::map<std::string,mystruct_1> _Pat;
static std::multimap<std::string,mystruct_2> map_occ;

switch( myswitch ) {
            case _P_1 :
        {
         while( condition_1 ){
            mystruct_1 *p =  new mystruct_1();
            _Pat.insert ( std::pair<std::string ,mystruct_1>(p->_name,*p) );
            }   
        }
        break;
            case _P_2 :
        {
        std::string name;
        while( condition_2 ){
            mystruct_2 *line=new mystruct_2();
            map_occ.insert ( std::pair<std::string ,mystruct_2>(name,*line) );  
        }
        break;
       case _P_3 :
        {
        // here I need to get what was stored
        myclass::method_2();

        }
        break;
       default :
        // something else ;

}
}
myclass::method_2(){
//uses what was stored in the map and in the multimap 
}

1 个答案:

答案 0 :(得分:1)

你似乎已经创造了某种“C with classes”怪物。

这些静态变量应该是您的类的成员,例如:

class foo
{
     public:
       std::string m_String; // Not defined globally or at file scope!
};

这样,每当你为同一个foo实例更改m_String时,它都会保持其状态。

相关问题