Class Initializer

Started by
1 comment, last by ApochPiQ 11 years, 8 months ago
I have a class with members as follows:


typedef void (*size_fn)(int, int);
typedef void (*key_fn)(InputType, int);
typedef void (*pointer_fn)(InputType, int, int);


typedef std::hash_map<const void*, size_fn> size_handle_c;
typedef std::hash_map<const void*, key_fn> key_handle_c;
typedef std::hash_map<const void*, pointer_fn> pointer_handle_c;

size_handle_c size_handles;
key_handle_c key_handles;
pointer_handle_c pointer_handles;


And am trying to initialize them in the constructor as follows:


EventManager* EventManager::instance = NULL;

EventManager::EventManager()
: size_handles(), key_handles(), pointer_handles()
{
instance = this;
}


Problem is, when I go to handle an event:


void EventManager::FirePointer(InputType type, int x, int y) const
{
if(pointer_handles.size() == 0) return;

pointer_handle_c::const_iterator i = pointer_handles.cbegin(); //Access violation
pointer_handle_c::const_iterator e = pointer_handles.cend();

while(i != e) i++->second(type, x, y);
}


I get an access violation at the indicated line. It looks like all my hash_maps are also basically uninitialized. Did I completely miss something in construction?
Advertisement
False alarm. The problem ended up being in a completely different place:

EventManager* EventManager::GetInstance()
{
if(EventManager::instance) EventManager::instance; //Should be if(EventManager::instance) return EventManager::instance;


EventManager();
return EventManager::instance;
}


Sigh...
For future reference, this code:

class Foo
{
Foo()
: Member_Bar()
{ }
};


Is precisely identical to this code:

class Foo { };

All of your members are default-constructed unless otherwise specified.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

This topic is closed to new replies.

Advertisement