Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I call a pointer-to-member-function?

I'm getting a compile error (MS VS 2008) that I just don't understand. After messing with it for many hours, it's all blurry and I feel like there's something very obvious (and very stupid) that I'm missing. Here's the essential code:

typedef int (C::*PFN)(int);

struct MAP_ENTRY
    {
    int id;
    PFN pfn;
    };

class C
    {
    ...
    int Dispatch(int, int);
    MAP_ENTRY *pMap;
    ...
    };

int C::Dispatch(int id, int val)
    {
    for (MAP_ENTRY *p = pMap; p->id != 0; ++p)
        {
        if (p->id == id)
            return p->pfn(val);  // <--- error here
        }
    return 0;
    }

The compiler claims at the arrow that the "term does not evaluate to a function taking 1 argument". Why not? PFN is prototyped as a function taking one argument, and MAP_ENTRY.pfn is a PFN. What am I missing here?

like image 760
chrisd Avatar asked Dec 18 '09 14:12

chrisd


People also ask

How do you use pointers to member functions?

Using a pointer-to-member-function to call a function Calling the member function on an object using a pointer-to-member-function result = (object. *pointer_name)(arguments); or calling with a pointer to the object result = (object_ptr->*pointer_name)(arguments);

How do I access the member of a pointer?

To access a member function by pointer, we have to declare a pointer to the object and initialize it (by creating the memory at runtime, yes! We can use new keyboard for this). The second step, use arrow operator -> to access the member function using the pointer to the object.

How do you call a function from a function pointer?

Function pointers in C need to be declared with an asterisk symbol and function parameters (same as function they will point to) before using them in the program. Declaration of function pointers in C includes return type and data type of different function arguments.

Which is referred by pointer to member?

Which is referred by pointers to member? a) Static members of class objects.


3 Answers

p->pfn is a pointer of pointer-to-member-function type. In order to call a function through such a pointer you need to use either operator ->* or operator .* and supply an object of type C as the left operand. You didn't.

I don't know which object of type C is supposed to be used here - only you know that - but in your example it could be *this. In that case the call might look as follows

(this->*p->pfn)(val)

In order to make it look a bit less convoluted, you can introduce an intermediate variable

PFN pfn = p->pfn;
(this->*pfn)(val);
like image 99
AnT Avatar answered Oct 12 '22 10:10

AnT


Try

return (this->*p->pfn)(val);
like image 23
Michael Krelin - hacker Avatar answered Oct 12 '22 09:10

Michael Krelin - hacker


Just to chime in with my own experience, I've come across an error in g++ caused by this statement:

  (this -> *stateHandler)() ;

Where stateHandler is a pointer to a void member function of the class referenced by *this. The problem was caused by the spaces between the arrow operator. The following snippet compiles fine:

(this->*stateHandler)() ;

I'm using g++ (GCC) 4.4.2 20090825 (prerelease). FWIW.

like image 42
Gearoid Murphy Avatar answered Oct 12 '22 09:10

Gearoid Murphy