pointer to a static function

To C++ experts,

If I have a class, and a function like :
class X{} ;
static X* fun(int*) ;

How do I declare a pointer to a function which can point to fun ?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Use of the word static for a function to prevent it from having public linkage is now deprecated (though I must confess I do it all the time). You should really use an unnamed namespace instead, if you want to prevent the function from having public linkage.

#include <iostream>

class X {};

namespace {
        class X x;
        X* fun(int*)
                return &x;

int main()
        // Get a pointer to the function
        X* (*ptr)(int*) = fun;

        // Use it
        int myint = 123;
        X* xp = (*ptr)(&myint);

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
int nData = 10;
X* (*pMyFunction)(int*);

pMyFunction = fun;
Exactly if there was no 'static' keyword - this only means the function is accessible only from this file. Even when you declare static function inside the class, the syntax would be the same.

X* (* tFunnc)(int*);

tFunc myFunc = fun;
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

meow00Author Commented:

    Could anyone please tell me that why not use
 "(static X*)(*pMyFunction)(int*);" ? why I don't need static in the very beginning ? Thanks !

'static' is a definition of a SCOPE of a function, not a TYPE, which is parameters and return value.

Exactly as:

class A{
   static int s_i;

the type of s_i is 'int' and not 'static int'
Try the following code snippet

int f () {
static int f () {

This would give u a compile error saying "redefinition of `int f()' "

This demonstrates that static is not at all related to the function signature

Now, technically, static is a type of storage class, thus it only decides what storage class the function belongs to and the behaviour of the compiler gets affected in the corresponding way

When u are declaring a pointer to a function, u just need the function signature and not the storage class of the function

That's the reason for your code to work


No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Split: rstaveley {http:#9737786} & migoEX {http:#9741331}

Please leave any comments here within the next seven days.

EE Cleanup Volunteer
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.