There is a problem in the simple code snippet below that i can't uncover even with a debugger. Thanks for your help in advance.
Command.h
class word{
private : static int next_word;
private : int word_id;
public : word();
public : void grabIt();
public : void raiseIt();
public : void putItDown();
};
class Command{
public : typedef void(word::*Method)();
private : word* w;
private : Method m;
public : Command(word *w, Method m);
public : void execute();
};
template<typename T>
class Queue{
private : enum{
SIZE=9
};
private : T* commandArray[SIZE];
private : int m_added = 0, m_removed = 0;
public : void enqueue(T* t){
commandArray[m_added] = t;
m_added = (m_added + 1) % SIZE;
}
public : T* dequeue(){
int temp = m_removed;
m_removed = (m_removed + 1) % SIZE;
return commandArray[temp];
}
};
Command.cpp
int word::next_word = 0;
word::word(){
this->word_id = next_word++;
}
void word::grabIt(){
std::cout << "Grabbed it" << std::endl;
}
void word::raiseIt(){
std::cout << "Raised it" << std::endl;
}
void word::putItDown(){
std::cout << "Put it down" << std::endl;
}
Command::Command(word *w, Method m){
w = w;
m = m;
}
void Command::execute(){
(w->*m)(); // -------------->>>>> Causing Seg-Fault
}
Main.cpp
Queue<Command> queue;
Command *commandArray[9];
Command command1(new word, &word::grabIt); commandArray[0] = &command1;
Command command2(new word, &word::raiseIt); commandArray[1] = &command2;
Command command3(new word, &word::putItDown); commandArray[2] = &command3;
Command command4(new word, &word::grabIt); commandArray[3] = &command4;
Command command5(new word, &word::raiseIt); commandArray[4] = &command5;
Command command6(new word, &word::putItDown); commandArray[5] = &command6;
Command command7(new word, &word::grabIt); commandArray[6] = &command7;
Command command8(new word, &word::raiseIt); commandArray[7] = &command8;
Command command9(new word, &word::putItDown); commandArray[8] = &command9;
for( int i=0 ; i < 9; i++){
queue.enqueue(commandArray[i]);
}
for( int i=2 ; i < 6 ; i++){
queue.dequeue()->execute();
}
Some word objects inside the queue appear NULL in the debugger, so cause Seg-Fault.