filling a static member map variable

This is a discussion on filling a static member map variable within the C++ Programming forums, part of the General Programming Boards category; Say I have a static map variable in a class. Where do I put the code to fill the map ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595

    filling a static member map variable

    Say I have a static map variable in a class. Where do I put the code to fill the map so all objects of the class type can use it?

    Code:
    class Round
    {
      private:
    	static std::map<const std::string, int> mapStringToInt;
      .....
      public:
    	 Round();
    };
    Here's the code I use to fill the map when it's not a static member variable:

    Code:
    const std::string IntToStr(int x)
    {
      if(x == 0)
    	return "w";
      else
    	return "b";
    }
     
    
    std::string referenceString, A, B, C, D;
    int a, b, c, d, num = 1;
     
    //generate referenceString first
    for(a = 0; a < 2; ++a)
    {
      A = intToStr(a);
      for(b = 0; b < 2; ++b)
      {
    	B = intToStr(b);
    	for(c = 0; c < 2; ++c)
    	{
    	  C = intToStr(c);
    	  for(d = 0; d < 2; ++C)
    	  { 
    		 D = intToStr(d);
    		 referenceString = A + B + C + D;
    		 
    		 //then map referenceString to num
    		 mapStringToInt[referenceString] = num++;
    	  }
    	}
      }
    }
    Also, is it possible to make the map unchangeable once it's created? Maybe something like this:

    const static std::map<const std::string, int> mapStringToInt;


    Thanks.

  2. #2
    Registered User glUser3f's Avatar
    Join Date
    Aug 2003
    Posts
    345
    You need to put map initializing code in a static function, and call it before creating objects.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Thank you. I'll give that a try.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Replies: 2
    Last Post: 04-19-2008, 12:06 AM
  3. global and static variable in a class delivered in a DLL
    By George2 in forum C++ Programming
    Replies: 16
    Last Post: 04-13-2008, 08:19 AM
  4. static variable vs. DLL
    By pheres in forum C++ Programming
    Replies: 11
    Last Post: 02-06-2008, 01:15 AM
  5. [GLUT] Pointers to class methods
    By cboard_member in forum C++ Programming
    Replies: 13
    Last Post: 02-16-2006, 03:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21