Having owning raw pointers is a bad practice in general (unless you are in special cases, like if you are building a custom advanced data structures, and the owning raw pointers are protected in proper RAII class boundaries).
The first option should be to use value semantics; if that is not good for your performance, then you may want to use smart pointers like shared_ptr (this helps make your code simpler, make it exception-safe, avoid resource leaks, etc.).
This is a sample compilable code, that seems to work on VS2010 SP1 (VC10):
#include <iostream> // for std::cout
#include <map> // for std::map
#include <memory> // for std::shared_ptr and std::make_shared
using namespace std;
typedef unsigned int DWORD;
struct MyClass
{
MyClass() : Data(0) {}
explicit MyClass(int data) : Data(data) {}
int Data;
};
int main()
{
//
// Build some test maps
//
typedef map<DWORD, shared_ptr<MyClass>> InnerMap;
typedef map<int, shared_ptr<InnerMap>> OuterMap;
auto innerMap1 = make_shared<InnerMap>();
(*innerMap1)[10] = make_shared<MyClass>(10);
(*innerMap1)[20] = make_shared<MyClass>(20);
OuterMap myMap;
myMap[30] = innerMap1;
//
// Testing code for maps access
//
const int keyOuter = 30;
auto itOuter = myMap.find(keyOuter);
if (itOuter != myMap.end())
{
// Key present in outer map.
// Repeat find in inner map.
auto innerMapPtr = itOuter->second;
const DWORD keyInner = 20;
auto itInner = innerMapPtr->find(keyInner);
if (itInner != innerMapPtr->end())
{
cout << itInner->second->Data << '\n';
}
}
}
std::map<int, std::map<DWORD,IDLL::CClass> > MyMap(without the pointers)?std::map<std::pair<int, DWORD>,IDLL::CClass*> my_mapmay work better for you. You can simple combine the two keys with astd::pair, and stop using pointers while your at it. Containers do not generally need to be declared on the heap.