473,408 Members | 2,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

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

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
Nov 17 '05 #1
3 1646
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" <su******@hotpop.com> wrote in message news:%2****************@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
Nov 17 '05 #2

"Bill Sun" <su******@hotpop.com> wrote in message news:%2****************@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
Nov 17 '05 #3
"Bill Sun" <su******@hotpop.com> wrote in message news:<#J**************@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 ?


// 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
Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: cppaddict | last post by:
I have class with two static member objects, one of type int and one of type vector<int>. static int myStaticMemberInt static vector<int> myStaticMemberVector; I know how to initialize the...
2
by: katekukku | last post by:
HI, Could anyone please tell me what are static variables and what exactly are there features. I am a little bit confused. Thank You
5
by: Jim Langston | last post by:
What I want to do: have a vector of ints in my class initialized with 0 to 499 which will later be pushed/popped out of the vector by instances. What I have: class CParticleStream // Yes, I...
5
by: Martin Vorbrodt | last post by:
so if i have this: // header.h class X { public: static int s = 1; }; i still need this:
6
by: markww | last post by:
Hi, I put a static member variable in my class. class CMine { static int m_nCount; }; How do I initialize it to zero? I can't do that in the constructor of the class can I? Won't that...
9
by: subramanian | last post by:
I am a beginner in C++. Suppose I want to build a class: I have given below the starting code: class Date { int day, month, year; static Date default_date; };
14
by: Jeroen | last post by:
Hi all, I've got a question about writing a library. Let me characterize that library by the following: * there is a class A which is available to the user * there is a class B that is used...
4
by: Bram Kuijper | last post by:
Hi all, as a C++ newbie, I got some question on the initialization of static reference data members. Since it isn't possible to initialize static members of a class in the constructor, I...
5
by: Timothy Madden | last post by:
Hy static members of non-integral type need to be declared in the class, but defined (and constructed or initialized) outside the class. Like this class SystemName { public:
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.