CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Aug 2001
    Posts
    19

    Angry How to initialize static map?

    How can I initialize a static map type member without an instance?

    For example:

    class MyClass{
    ...
    const static CMap<int,int,int,int> m_map;
    }

    const CMap<int,int,int,int> MyClass::m_map;
    MyClass::m_map.SetAt(1,1); << Error
    MyClass::m_map.SetAt(2,2); << Error

    I need a static map (dictionary) in the class which is accessible by static function.

    Thanks

  2. #2
    Join Date
    Mar 2002
    Location
    California
    Posts
    1,582
    Initialize it the first time the static method is called, as in:

    Code:
    static MyMap& getMap()
    {
         static bool firstCall = true;
         if( firstCall )
         {
              firstCall = false;
              // init map here
         }
    
         return m_map;
    }
    Jeff

  3. #3
    Join Date
    May 2002
    Location
    Quebec City, Canada
    Posts
    374
    user a static member method to get a reference to the map instance.

    Code:
    //static 
    CMap<int,int,int,int> & MyClass::getMap()
    {
         return m_map;
    }

    Btw, since this is a non-MFC forum, why don't you use the standard STL map?
    Last edited by proxima centaur; June 26th, 2002 at 02:37 PM.
    Martin Breton
    3D vision software developer and system integrator.

  4. #4
    Join Date
    Aug 2001
    Posts
    19

    Smile

    Thanks for your good idea. It solves my problem.

    Sonny :-)

  5. #5
    Join Date
    Jun 2002
    Posts
    137
    const CMap<int,int,int,int> MyClass::m_map;
    MyClass::m_map.SetAt(1,1); << Error
    MyClass::m_map.SetAt(2,2); << Error
    you declare that the m_map as const, can you modify it by SetAt()? That is the reason why return the reference of it by getMap() works since it removed the const implicitly.

  6. #6
    Join Date
    Jun 1999
    Location
    San Diego, CA
    Posts
    600

    Const data CAN NOT be modified!!!

    None of previous follow ups is correct.

    You can NOT modify any data that is declared as const. As a matter of fact, if memory storage is allocated for any const data, it will be put in a section of memory that is read-only, trying to write to that memory to change the data will cause a write protected memory exception.

    You can cast to none-const pointer, return a reference, or do other "smart" thing to remove the constness, but the fact is the data is in write protected memory location so any attempt to modify them will fail.

    Also what if your const CMap class stored hundreds of thousands of items. Do you manually code it to add all the items?

    The only way of initializing a const class or structure, is to use the old C style way of initialize structures when declaring. Like this:
    class MC
    {
    public:
    int m_data1;
    int m_data2;
    };

    const MC gMC = {1,2};

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured