How to initialize a static std :: map?

I created a message-only window class, and I'm trying to map the HWND back to objects with these descriptors. I am trying to do this using a private static std::map<HWND, CMyClass*>belonging to a class, for example:

MyClass.h:

class CMyClass
{
    ...

private:
    HWND        m_hWnd;
    HINSTANCE   m_hInstance;
    LPCSTR      m_szClassName;

    static std::map<HWND, CMyClass*> s_mapHandles;

    ...
};

MyClass.cpp:

std::map<HWND, CMyClass*> CMyClass::s_mapHandles;

but when I try to add to the map, the program crashes. I tried three different forms and they all give the same error:

...
m_hWnd = ::CreateWindowEx(0, m_szClassName, "Message Window", 0, 0, 0, 0, 0, HWND_MESSAGE, 0, m_hInstance, 0);
s_mapHandles.insert(pair<HWND, CMyClass*>(m_hWnd, this));

or

...
s_mapHandles.insert(s_mapHandles.end(), pair<HWND, CMyClass*>(m_hWnd, this));

or even

...
s_mapHandles[m_hWnd] = this;

In each case, a call fails _Root(), which tries to return _Parent(_Myhead); _Parent(_Myhead)returns (_Nodepref)(*_Myhead)._Parentwhich fails because it _Myheadis null.

How to initialize a card so that its head is not zero, and I can insert things without failing? Sorry if I explained this poorly - I'm new to C ++.

+3
6

?

++ FAQ Lite - 10.12 " " ?

+6

, .

+1

. ? , null, .

+1

, ( ). , . . , . , .

+1

++ , , - .cpp . , , , . , , - - .

0

This was probably solved at the same time, but just for reference: here is another solution for the actual problem that is behind the question: you can store user data in the GWL_USERDATA field of any window (I believe using the API :: SetWindowLong API if I remember correctly). If you put your CMyClass pointer in it instead of linking it to HWND via a map, well, you don’t need a map at all, and it’s more efficient, since all you have to do is give the pointer an expensive map search instead.

0
source

Source: https://habr.com/ru/post/1713656/


All Articles