Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / June 2004

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Bill Sun - 28 Jun 2004 11:10 GMT
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] - 28 Jun 2004 14:44 GMT
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

 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 - 28 Jun 2004 15:38 GMT
 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 - 28 Jun 2004 17:23 GMT
> Hi,
>
[quoted text clipped - 16 lines]
> 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

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.