Ok, this is veeery weird... I think. What I mean with the title is:
inside the act() function from an actionHandler object I have:
state->getHumanPieces();
Which gives me an address violation of some sort, apparently 'this' does not have a 'state' variable initialized... It so happens this actionHandler class has a static variable, which is a pointer to an instance of itself, called 'handler'... and if I do:
handler->state->getHumanPieces();
It works perfectly.. In order to make this even clearer:
That 'handler' pointer, points to the only instance of actionHandler existing in the whole program (singleton pattern).. So basically when I run this act() function from my actionHandler object, it doesn't let me access the 'state' variable, BUT if from that object, I try to access the same variable through a pointer to the same object, it is ok?? I don't get what is going on.. I'm not sure if it is clear, prob a bit confusing, but I hope it is understandable..
Btw, the VS08 debugger is showing what I mean:
this: 0x000000 {state=???}
handler: someAddress {state= someAddress}
handler:...
state:...
state: CXX0030: ERROR: expression cannot be evaluated
I hope that makes it clearer, it's the little tree-structure that shows up on the little window where the current values of the variables are shown (Autos).
EDIT: I so get that the this pointer is null, I just don't understand how it can be null.. I'll post some code:
actionHandler.h:
class gameState;
class actionHandler
{
public:
static actionHandler* Instance(){return handler;}
void act(int,int);
private:
actionHandler();
static actionHandler* handler;
gameState *state;
};
actionHandler.cpp:
actionHandler* actionHandler::handler = new actionHandler();
actionHandler::actionHandler()
{
state = gameState::Instance();
}
void actionHandler::act(int x, int y)
{
state->getHumanPieces();
}
now, in gameState.h i have a similar structure(singleton) and an actionHandler* private var, which gets initialised in:
gameState::gameState()
{
handler = actionHandler::Instance();
}
and also a getHandler() func which returns the handler. This all should get initialised in main.cpp:
gameState *currState = gameState::Instance();
actionHandler *handler = currState->getHandler();
and then is used:
handler->act(event->button.x,event->button.y);
main.cpp is writen in simple .c style, with no header, so yes I suppose the fucntion calling the handler is static... however, I also make calls to the gameState* pointer, which supposedly works exactly in the same way as the actionHandler* one.. Hope this makes it more clear