Jump to content
  • Advertisement
Sign in to follow this  
Silly_con

Global class of function pointers

This topic is 4848 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 want a global instance of a class with function pointers, so I can access to it in all files and all places of a project, my question is, where I declare it, and were I initialize it (assign functions to the functions pointers) example:
class Tools { public: void (*foo)(int); }; // without cons/dest,private and get/set for simplicity

in all files, I want call Tools.foo(1), and assign other functions to func pointer too (in real time). Something like java Math.sin, Math.sqrt ... thanks

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Something like java Math.sin, Math.sqrt ...

These are not function pointers, these are static methods. Just for clarification, the equivalent in C++ would be :

class Math
{
public:
static double pow(int base, int power)
{
// do something magical here
// return the result
}
};

// use the function
// somewhere else
void CallFunc()
{
double d = Math::pow(2, 3);
}



Share this post


Link to post
Share on other sites
Using the . operator instead of the scope resolution operator? If so its fairly asinine, and I'm real nitpicky about syntax. Consider it a lingual flaw.

That out of the way, this should work:

// Math.hpp
class MathImpl
{
public:
void Foo() { }
};

// MSVC specific extension to avoid multiple definition link errors
__declspec(selectany) MathImpl Math;

void main()
{
Math.Foo();
}

Share this post


Link to post
Share on other sites
A function pointer is really only useful for 2 things. Call back functions and arrays of functions. You havn't really what the purpose of this is and in what fashion you want to be able to use it, but it seems that what Stormrunner said is what you want.

Share this post


Link to post
Share on other sites
Quote:
Original post by Eps
A function pointer is really only useful for 2 things. Call back functions and arrays of functions. You havn't really what the purpose of this is and in what fashion you want to be able to use it, but it seems that what Stormrunner said is what you want.


Static methods of classes can be referred to in practically the same way as C function pointers:


class foo
{
public:
static void bar() //Static method
{
...
}

void doit() //Instance method
{
...
}

};

void bar()
{
...
}


In the above snippet, the function pointer "foo::bar" can be used in the same way as the function pointer "bar".

AFAIK, to call an instance method of a class, you need to be able to refer to an instance in some way, either through a pointer or a reference.

[Edited by - Couvillion on February 11, 2005 9:33:31 AM]

Share this post


Link to post
Share on other sites
I want to use a function pointer, because I want to change the implementation of the function called in real time, so a pointer that points to different functions along the execution of the program is the best option.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!