0

some library have code like this:

typedef unsigned long(*poFunc)();
poFunc T ;
//...

void setupA(poFunc exFunc)
{
    T = exFunc;
}

and I can use it like this:

unsigned long run()
{
    return 9929UL;
}

int main()
{
    setupA(run);
}

Now I need to group the functions in a class like this:

class A
{
    private:
        unsigned long run();
    public:
        void start ();
};

unsigned long A::run()
{
    return 9929UL;
}

void A::start ()
{
    setupA(&A::run);
}

int main()
{
    A _a;
    _a.start();
}

and I get this error:

error: cannot convert 'long unsigned int (A::*)()' to 'poFunc {aka long unsigned int (*)()}' for argument '1' to 'void setupA(poFunc)'

I found this issue similar to my problem,but I have no idea how to fix this.

Thanks.

Community
  • 1
  • 1
xpcn
  • 1
  • 1
  • 1
  • 2
    A pointer to a member function is not the same as a pointer to a non-member function. Roughly speaking, member functions have a hidden argument (that becomes the `this` pointer), and non-member functions doesn't have this hidden argument. I recommend you read about [`std::function`](http://en.cppreference.com/w/cpp/utility/functional/function) and [`std::bind`](http://en.cppreference.com/w/cpp/utility/functional/bind), [here I have a simple example](http://stackoverflow.com/questions/14189440/c-class-member-callback-simple-examples/14189561#14189561) on how to use those. – Some programmer dude Jan 15 '16 at 13:38
  • Classes don't exist merely to "group functions"; they exist to provide per-instance data (state) upon which functions (methods) can act. I assume your example is simplified and has per-instance data in reality, but just in case... It is not recommended to use classes purely as semantic grouping mechanisms. (unless coding Java, where you're given no choice...) `static` functions have no associated instance but should still only be used when they have a relevant coupling to the non-`static` stuff. Otherwise, just use a `namespace`. – underscore_d Jan 15 '16 at 14:32

2 Answers2

0

Declare the A::run method as static .

The following compiles and executes correctly.

typedef unsigned long(*poFunc)();
poFunc T ;

void setupA(poFunc exFunc)
{
    T = exFunc;
}
class A{

    poFunc T ;
    private:
        static  unsigned long run();
    public:
        void start ();

};

unsigned long A::run()
{
    return 9929UL;
}

void A::start ()
{
    setupA(&A::run);
}

int main()
{
    A _a;
    _a.start();

    printf("END\n");
}

Hope this helps out.

Davide Spataro
  • 7,319
  • 1
  • 24
  • 36
0

You can introduce your class before defining your new type (function pointer) by statement typedef.

class A;
typedef unsigned long(A::*poFunc)();

In typedef statement then you have to make clear that you want to point to a function that is defined in your class.

But I am not sure that this a good style of programming. You avoid the concept of classes. With your function pointer you are able to call a function that is declared as private from outside the class!

cppmathe
  • 1
  • 1