0

A want to create some kind of global memory table.
Every time when I create a specific kind of object,
I wolud like to register it in the table, and save some data.
Let assume that this "memory table" is a map,
and a global variable.
Let assume that I have lots of objcets from the same class.
The constructor of these objects
creates a new data entry in the memory table,
and the destructor erase it.
I want that the objects can acces to the table and use it.
My problem is: what kind of map is good for me?
Because I dont want to copy the objects,
I just want to map: obejct reference (or pointer?) -> data
What is the syntax?

2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by merse
0

You can create a map of "Specific Object" pointers.
E.g.:

class CMemoryMap
{
public:
  void newObj( const CObject& myObject )
  {
       m_mMemoryMap.insert( std::pair<CObject*, some_data>( &myObject, some_data ) );
  }
  void delObj( const CObject& myObject )
  {
        map_iterator  = m_mMemoryMap.find( myObject );
        m_mMemoryMap.erase( map_iterator );
  }
  private:
       map<CObject*, some_data> m_mMemoryMap;
};

Of course this code won't compile and a lot more sanity checking should be done.. but its just an idea.

So you actually store a pointer, not the actual object.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.