substitute for C++ static keyword

Hi,
Is there a way to get a map or a list in c++ to keep its old entries with several calls without being defined as static.
In fact,I'm using a multi map, the problem is that if it's static, it returns only the first entry, else I just end up with an empty map.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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 
}
Last edited on
Since _Pat and map_occ are only used in myclass::method1(), you could make them members of myclass.

By the way, your code for myclass::method1() leaks.
It should be
1
2
3
mystruct_1 *p =  new mystruct_1();
_Pat.insert ( std::pair<std::string ,mystruct_1>(p->_name,*p) );
delete p;

or even better
1
2
mystruct_1 p();
_Pat.insert ( std::pair<std::string ,mystruct_1>(p.name,p) );


EDIT :
as ne555 mentions it in the next post, it should be
1
2
mystruct_1 p;
_Pat.insert ( std::pair<std::string ,mystruct_1>(p.name,p) );
Last edited on
mystruct_1 p(); is a function declaration
mystruct_1 p; declares an object
Topic archived. No new replies allowed.