static function pointer inside namespace not working in specific source file.

Started by
1 comment, last by owl 16 years, 11 months ago
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.
[size="2"]I like the Walrus best.
Advertisement
Note that static outside a class definition means a different thing. In this particular case, it creates two variables, one for each *.cpp. You might want to look into extern instead.
Thanks. That's surely why the variable doesn't look initialized inside Update().
[size="2"]I like the Walrus best.

This topic is closed to new replies.

Advertisement