• Advertisement
Sign in to follow this  

Class Initializer

This topic is 2026 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement