Jump to content
  • Advertisement
Sign in to follow this  
owl

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

This topic is 4161 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
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.

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!