Sign in to follow this  

Class Initializer

This topic is 1953 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:

[CODE]
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;
[/CODE]

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

[CODE]
EventManager* EventManager::instance = NULL;

EventManager::EventManager()
: size_handles(), key_handles(), pointer_handles()
{
instance = this;
}
[/CODE]

Problem is, when I go to handle an event:

[CODE]
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);
}
[/CODE]

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
False alarm. The problem ended up being in a completely different place:

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


EventManager();
return EventManager::instance;
}
[/CODE]

Sigh...

Share this post


Link to post
Share on other sites
For future reference, this code:

[code]class Foo
{
Foo()
: Member_Bar()
{ }
};[/code]

Is precisely identical to this code:

[code]class Foo { };[/code]

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

Share this post


Link to post
Share on other sites

This topic is 1953 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this