I basically have a class that takes a function pointer in its constructor, and then stores it in a variable to be used when a function is called on the class, like this:
class foo{ public: foo( void(*a)() = 0 ): func(a){} void call(){ if(func) func(); } private: void(*func)(); }; The problem occurs when I create an instance with a function that has been forward declared in an included header. By debugging I found out that the pointer func is sometimes null, while the function passed is valid, causing it not to be executed.
I'm using CodeBlocks with MinGW.
So like this:
function.h
void test(); function.cpp
#include "function.h" void test(){ std::cout << "Hello World" << std::endl;} main.cpp
#include "function.h" int main(){ foo a(test); a.call(); return 0;} One more thing: it doesn't always happen wrongly, so I think it has to do with optimisations or something like that.
Some things I left out because they shouldn't matter is that the foo instance is created with new, added to a std::vector and deleting itself at the end of the call() function. The destructor makes sure it is also erased from the std::vector. That all happens in a different cpp file that also includes function.h
SOLVED
fooobjects.