static const map

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • elhoushi
    New Member
    • Mar 2009
    • 4

    static const map

    Dear all,

    I have found a turnaround solution to the problem of creating a static const map for those who want to make large lookup tables.

    Form a class and make the map an instance member of that class. Fill the map in the class' constructor.

    After that, make an static instance of that class.

    In this way the data is filled only once. And you may define a method to return a value from the map.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Actually, you do not wnat a static instance of the map<>.

    What you need is a Singleton class that manages the map<>.

    Read this: http://bytes.com/topic/c/insights/65...erns-singleton.

    Comment

    • elhoushi
      New Member
      • Mar 2009
      • 4

      #3
      Yes. I have used a singleton.
      Here is the implementation:

      ObjectMap.h file:

      #include <string>
      #include <map>
      using namespace std;

      class ObjectMap
      {
      public:
      static ObjectMap* GetInstance();
      string GetName(unsigne d int number) {return mapInstance->m_map[number];}

      protected:
      ObjectMap();
      map<unsigned int, string> m_map;

      private:
      static ObjectMap* mapInstance;
      };
      ObjectMap.cpp file:
      #include "ObjectMap. h"

      ObjectMap* ObjectMap::mapI nstance = NULL;

      //_______________ _______________ _______________ ____
      ObjectMap* ObjectMap::GetI nstance()
      {
      if (mapInstance == NULL)
      mapInstance = new ObjectMap;
      return mapInstance;
      }

      //_______________ _______________ _______________ ____
      ObjectMap::Obje ctMap()
      {
      m_map[0] = "Hello World0";
      m_map[1] = "Hello World1";
      m_map[2] = "Hello World2";
      }

      You may then create an instance of the ObjectMap class using the new statement and don't delete the pointer so that it will be available all the time and won't need to re-create it.

      #include ObjectMap.h

      void main()
      {
      ObjectMap* c = new ObjectMap;
      c->GetName(2);
      }

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        ObjectMap is not a singleton because you can create many instances of ObjectMap. Each of these instances will compete for your static map.

        A Singleton is a class that can have only one instance.

        Again, I say, the map should not be static. It should be just a regular member variable. ObjectMap needs to be set up so that only one instance can be created and that way you have one map.

        You have no Instance() method to get a pointer to the Singleton.

        Read this and pay special attention to a Singleton registry: http://bytes.com/topic/c/insights/65...erns-singleton.

        Comment

        • elhoushi
          New Member
          • Mar 2009
          • 4

          #5
          Yes, you are right considering the point that there is no need to form a static instance of the class.

          You have no Instance() method to get a pointer to the Singleton.
          However, the class I have formed is a singleton. I have a GetInstance() method which is the Instance() method you are looking for.

          Comment

          • elhoushi
            New Member
            • Mar 2009
            • 4

            #6
            I am sorry, this part is wrong:
            #include ObjectMap.h

            void main()
            {
            ObjectMap* c = new ObjectMap;
            c->GetName(2);
            }

            It should be:

            #include ObjectMap.h

            void main()
            {
            ObjectMap* c = ObjectMap::GetI nstance();
            c->GetName(2);
            }
            I wanted to edit the original post but I couldn't.

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              That being the case, the map need not be static since there is only one ObjectMap object.

              Comment

              Working...