I have 3 files:
main.cpp
#include mynamespace.h
void real_func()
{
std::cout << "it works";
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
mynamespace::ptr_func = real_func;
mynamespace::Update();
}
mynamespace.h
namespace mynamespace
{
void Update();
typedef void (*FPTR)(void)=0;
static FPTR ptr_func = 0;
}
mynamespace.cpp
#include mynamespace.h
mynamespace::Update()
{
ptr_func();
}
The call to ptr_func() in Update() throws a Segmentation Fault error.
If I place ptr_func inside a class it works. It doesn't work when it is placed directly inside the namespace.
It seems there is some knowledge about namespaces, static members and function pointers that I'm missing.
Any help will be appreciated. Tnx.