473,772 Members | 2,564 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1664
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_map Array;

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

static mapLoader loader(mapClass ::s_mapArray);

-cd
"Bill Sun" <su******@hotpo p.com> wrote in message news:%2******** ********@TK2MSF TNGP12.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******@hotpo p.com> wrote in message news:%2******** ********@TK2MSF TNGP12.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******@hotpo p.com> wrote in message news:<#J******* *******@TK2MSFT NGP12.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 CMyMapInitializ er
{
public:
CMyMapInitializ er(map<string, int> &myMap)
{
myMap["Item01"] = 0;
myMap["Item02"] = 1;
}
};

map<string, int> mapclass::s_map Array;

CMyMapInitializ er mapInitializer( mapclass::s_map Array);

// 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
2889
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 int member: MyClass::myStaticMemberInt = 99;
2
1877
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
4573
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 know you wouldn't use 'C' { private: static std::vector<int> PSArray; public:
5
4288
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
17446
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 keep setting it to zero everytime a new
9
27384
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
2581
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 in severel 'underwater operations' * there is a list which stores objects of class B There are several issues I'm not sure about:
4
8368
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 should initialize them in advance. However, the following code, in which I first produce two classses and then try to assign a reference of the first class to a static data member of the second class doesn't work. It gives the following compiler error:
5
7240
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
9621
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10264
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10106
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9914
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8937
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5355
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5484
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4009
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3610
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.