user180574
user180574

Reputation: 6084

C++ - Issue of using function pointer to member function

I've applied solutions based on some search made, but the problem still there. Thank you so much for the help.

error: must use '.*' or '->*' to call pointer-to-member function ...

source code:

#include <stdio.h>

class A
{
  public:

    struct data;
    typedef int (A::*func_t)(data *);
    typedef struct data
    {
      int i;
      func_t func;
    }
    data;

    data d;

    void process()
    {
        d.func(&d);
    }

    A()
    {
        d.i = 999;
        d.func = &A::print;
    }

    int print(data *d)
    {
        printf("%d\n", d->i);
        return 0;
    }

};

int main()
{
    A *a = new A;

    a->process();

    return 0;
}

Upvotes: 0

Views: 211

Answers (5)

nishantjr
nishantjr

Reputation: 1820

Calling pointer the members require the class it is a member of to be the this param.

Try:

A a;
a.*(d.func)(&d);

Upvotes: -2

Sarfaraz Nawaz
Sarfaraz Nawaz

Reputation: 361482

d.func(&d);

is not enough. func is a member-function-pointer which is pointing to a non-static member of A. So it can be invoked on an object of A. So you need to write this:

(this->*(d.func))(&d);

That would work as long as you write this inside A.

If you want to execute func from outside, say in main(), then the syntax is this:

A a; 
(a.*(a.d.func))(&a.d);

That is an ugly syntax.

Upvotes: 3

Jonathan Wakely
Jonathan Wakely

Reputation: 171303

Other answers have already said you need to say (this->*d.func)(&d) to call a pointer-to-member function (because you need to provide the object that it's a member of)

Another option is to make the function a static function, which doesn't need special syntax to call. To do that, change the typedef like so:

typedef int (*func_t)(data *);

Then make the print function static:

static int print(data *d)
{
    ...
}

Now you can just call d.func(&d)

Upvotes: 0

Joseph Mansfield
Joseph Mansfield

Reputation: 110658

Your process function attempts to call d.func but it is a pointer to member function. A pointer to member function must be called on some object. Presumably you want the instance of A to be this, in which case your process function should look like:

void process()
{
    (this->*(d.func))(&d);
}

Note the use of the ->* operator to call a member function when you have a pointer to it.

Upvotes: 1

Some programmer dude
Some programmer dude

Reputation: 409196

Unfortunately what you are trying to do will not be possible, the reason being that print is not a static member function. This means it expects an implicit first argument that is the this pointer.

I suggest you try using the std::function and std::bind function, something like this:

class A
{
    struct data
    {
        std::function<void(const data&)> func;
        int i;
    };

    data d;

public:
    A()
    {
        d.func = std::bind(&A::print, *this);
        d.i = 999;
    }

    void process()
    {
        d.func(d);
    }

    void print(const data& my_data)
    {
        std::cout << my_data.i << '\n';
    }
};

Of course, since the print function now have a proper this pointer, you no longer need to pass the data structure to it:

class A
{
    struct data
    {
        std::function<void()> func;
        int i;
    };

    data d;

public:
    A()
    {
        d.func = std::bind(&A::print, *this);
        d.i = 999;
    }

    void process()
    {
        d.func();
    }

    void print()
    {
        std::cout << d.i << '\n';
    }
};

Upvotes: -1

Related Questions