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
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};