Class function pointer does not evaluate to a function 
Author Message
 Class function pointer does not evaluate to a function

Hello,

I'm using Visual Studio .NET and I would like to do the following, which I'm
fairly certain it's legal; (there's even an example of it in MSDN!) But I'm
getting error C2064: term does not evaluate to a function instead:

class A
{
    public:
    int AsFunc(int f) { return f; }
    int (A::*pfn)(int);

Quote:
};

void main()
{
    A a;
    a.pfn = &A::AsFunc;
    cout << a.pfn(1.5);        // Error C2064

Quote:
}

Does anyone know how to do this properly? Or should this work?

Thanks in advance,
        Mark



Sun, 18 Sep 2005 14:25:55 GMT  
 Class function pointer does not evaluate to a function



Quote:
> I'm using Visual Studio .NET and I would like to do the following, which
I'm
> fairly certain it's legal; (there's even an example of it in MSDN!) But
I'm
> getting error C2064: term does not evaluate to a function instead:

> class A
> {
>     public:
>     int AsFunc(int f) { return f; }
>     int (A::*pfn)(int);
> };

> void main()
> {
>     A a;
>     a.pfn = &A::AsFunc;
>     cout << a.pfn(1.5);        // Error C2064
> }

> Does anyone know how to do this properly? Or should this work?

You're missing the closure. "closure" means that you have to bind a pointer
to member function with an instance to be able to actually perform the call.

a.pfn is a pointer to member function. The fact that it's also a member of A
it doesn't mean that it's automatically bound to an instance. Let me
clarify: if you define it outside, the correct code is:

class A
{
public:
   int AsFunc(int f) { return f; }

Quote:
};

int (A::*pfn)(int);

int main(void)
{
    A a;
    pfn = &A::AsFunc;
    cout << (a.*pfn)(1);

Quote:
}

Notice the use of operator ".*" instead of operator "." to bind a pointer to
member function to an instance of the class (it's the closure).
If you keep pfn within the class definition, like in your example, you
should modify your code as: "(a.*(a.pfn))(1)", since you're calling operator
.* to bind the instance "a" to the pointer of member function "a.pfn".

Of course, it looks ugly. You should be using a different approach, and
probably also some binder facilities (look into Boost's bind and Boost's
function for example).

Giovanni Bajo



Mon, 19 Sep 2005 00:32:03 GMT  
 Class function pointer does not evaluate to a function
Makes sense. :)

Thanks for your response.

   Mark

Quote:

> You're missing the closure. "closure" means that you have to bind a pointer
> to member function with an instance to be able to actually perform the call.

> a.pfn is a pointer to member function. The fact that it's also a member of A
> it doesn't mean that it's automatically bound to an instance. Let me
> clarify: if you define it outside, the correct code is:

> class A
> {
> public:
>    int AsFunc(int f) { return f; }
> };

> int (A::*pfn)(int);

> int main(void)
> {
>     A a;
>     pfn = &A::AsFunc;
>     cout << (a.*pfn)(1);
> }

> Notice the use of operator ".*" instead of operator "." to bind a pointer to
> member function to an instance of the class (it's the closure).
> If you keep pfn within the class definition, like in your example, you
> should modify your code as: "(a.*(a.pfn))(1)", since you're calling operator
> .* to bind the instance "a" to the pointer of member function "a.pfn".

> Of course, it looks ugly. You should be using a different approach, and
> probably also some binder facilities (look into Boost's bind and Boost's
> function for example).

> Giovanni Bajo



Wed, 21 Sep 2005 06:49:17 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Using a pointer to a class function but not evaluating correctly

2. How to use (pointer to function), and function and pointer to (pointer to function)

3. Evaluating functions using IDebugger Evaluate member.

4. error C2064: term does not evaluate to a function

5. why does this term not evaluate to a function

6. function pointers to functions in classes.

7. Function pointer to function inside class

8. how to let a global function pointer point to a function in a Class

9. Pointer Functions and Pointers to Pointer Functions

10. Question about signal()/pointers to functions that return pointers to functions

11. function pointers and function pointers array

12. C++ function pointers versus C function pointer problem

 

 
Powered by phpBB® Forum Software