Sign in to follow this  
owl

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

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Thanks. That's surely why the variable doesn't look initialized inside Update().

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this