Connecting Tech Pros Worldwide Help | Site Map

How to initialize a static map member in a cpp file body

Bill Sun
Guest
 
Posts: n/a
#1: Nov 17 '05
Hi,

I have a question about to initialize a static map member like this:

In the mapclass.h;
class mapclass
{
private:
static map<string, int> s_mapArray;
}

In the mapclass.cpp;
.....
s_mapArray["Item01"] = 0;
s_mapArray["Item02"] = 1;
.....

I don't want to initialize this static member in a member function body, I only want to initialize it in the cpp file body.
What I can do ?

Thanks advanced,

Bill
Carl Daniel [VC++ MVP]
Guest
 
Posts: n/a
#2: Nov 17 '05

re: How to initialize a static map member in a cpp file body


You can't do what you want directly - the're no initializer syntax for something like a map.

What you can do is create another class to help you..

// in mapclass.h
class mapclass
{
private:
friend class maploader;
map<string,int> s_mapArray;
};

// In mapclass.cpp
#include "mapclass.h"

map<string,int> mapclass::s_mapArray;

class mapLoader
{
public:
mapLoader()
{
mapclass::s_mapArray["Item01"] = 0;
mapClass::s_mapArray["Item02"] = 1;
// ...
}
};

static mapLoader loader(mapClass::s_mapArray);

-cd


"Bill Sun" <sunshang@hotpop.com> wrote in message news:%23JlzzePXEHA.3512@TK2MSFTNGP12.phx.gbl...
Hi,

I have a question about to initialize a static map member like this:

In the mapclass.h;
class mapclass
{
private:
static map<string, int> s_mapArray;
}

In the mapclass.cpp;
....
s_mapArray["Item01"] = 0;
s_mapArray["Item02"] = 1;
....

I don't want to initialize this static member in a member function body, I only want to initialize it in the cpp file body.
What I can do ?

Thanks advanced,

Bill
Jeff F
Guest
 
Posts: n/a
#3: Nov 17 '05

re: How to initialize a static map member in a cpp file body



"Bill Sun" <sunshang@hotpop.com> wrote in message news:%23JlzzePXEHA.3512@TK2MSFTNGP12.phx.gbl...
Hi,

I have a question about to initialize a static map member like this:

In the mapclass.h;
class mapclass
{
private:
static map<string, int> s_mapArray;
}

In the mapclass.cpp;
....
s_mapArray["Item01"] = 0;
s_mapArray["Item02"] = 1;
....

I don't want to initialize this static member in a member function body, I only want to initialize it in the cpp file body.
What I can do ?

Thanks advanced,

Bill
I think this may be covered by the upcoming assignment library which is due in the next version? of boost. See www.boost.org.

Jeff F
Vinayak Raghuvamshi
Guest
 
Posts: n/a
#4: Nov 17 '05

re: How to initialize a static map member in a cpp file body


"Bill Sun" <sunshang@hotpop.com> wrote in message news:<#JlzzePXEHA.3512@TK2MSFTNGP12.phx.gbl>...[color=blue]
> Hi,
>
> I have a question about to initialize a static map member like this:
>
> In the mapclass.h;
> class mapclass
> {
> private:
> static map<string, int> s mapArray;
> }
>
> In the mapclass.cpp;
> ....
> s mapArray["Item01"] = 0;
> s mapArray["Item02"] = 1;
> ....
>
> I don't want to initialize this static member in a member function body,
> I only want to initialize it in the cpp file body.
> What I can do ?[/color]

// One of the few possible solutions.

class CMyMapInitializer
{
public:
CMyMapInitializer(map<string, int> &myMap)
{
myMap["Item01"] = 0;
myMap["Item02"] = 1;
}
};

map<string, int> mapclass::s_mapArray;

CMyMapInitializer mapInitializer(mapclass::s_mapArray);

// other mapclass definitions .....


-hth.
-Vinayak
Closed Thread