Connecting Tech Pros Worldwide Forums | Help | Site Map

Static Map

utab
Guest
 
Posts: n/a
#1: May 26 '06
Dear all,

In the private data section of my class I have defined a

static map<string, map<string,int> > Ent_fname_fid;

and in the implementation file I wanted to initialize it like a
built-in static variable
(if I gave a static int a in the class then I would have to write int
Classname::a=0)

This is not possible with the below code

map<string, map<string,int> > classname::Ent_fname_fid;

Ent_fname_fid["GRID"].insert(make_pair("GRID",1));
Ent_fname_fid["GRID"].insert(make_pair("ID",2));
Ent_fname_fid["GRID"].insert(make_pair("CP",3));
Ent_fname_fid["GRID"].insert(make_pair("X1",4));
Ent_fname_fid["GRID"].insert(make_pair("X2",5));
Ent_fname_fid["GRID"].insert(make_pair("X3",6));
Ent_fname_fid["GRID"].insert(make_pair("CD",7));
Ent_fname_fid["GRID"].insert(make_pair("PS",8));
Ent_fname_fid["GRID"].insert(make_pair("SEID",9));

This code is not in a function just outside all function definitions.

Regards,


Victor Bazarov
Guest
 
Posts: n/a
#2: May 26 '06

re: Static Map


utab wrote:[color=blue]
> In the private data section of my class I have defined a
>
> static map<string, map<string,int> > Ent_fname_fid;
>
> and in the implementation file I wanted to initialize it like a
> built-in static variable
> (if I gave a static int a in the class then I would have to write int
> Classname::a=0)
>
> This is not possible with the below code
>
> map<string, map<string,int> > classname::Ent_fname_fid;
>
> Ent_fname_fid["GRID"].insert(make_pair("GRID",1));
> Ent_fname_fid["GRID"].insert(make_pair("ID",2));
> Ent_fname_fid["GRID"].insert(make_pair("CP",3));
> Ent_fname_fid["GRID"].insert(make_pair("X1",4));
> Ent_fname_fid["GRID"].insert(make_pair("X2",5));
> Ent_fname_fid["GRID"].insert(make_pair("X3",6));
> Ent_fname_fid["GRID"].insert(make_pair("CD",7));
> Ent_fname_fid["GRID"].insert(make_pair("PS",8));
> Ent_fname_fid["GRID"].insert(make_pair("SEID",9));
>
> This code is not in a function just outside all function definitions.[/color]


Correct. This is not possible. So, put it in a function and init
a dummy int variable with that function:

int dummy = function_to_fill_the_map(Ent_fname_fid);

The function should take one argument - a reference to non-const
map of yours. In the body of the function you will put all those
'insert' things. Return 0 from the function.

V
--
Please remove capital As from my address when replying by mail


Closed Thread