Function Pointer for Member Function 
Author Message
 Function Pointer for Member Function

Hi,
I have declared a function pointer for the member function of a class, how
do I call the function using the pointer?

void CBoxesDlg::TestFn(void)
{
  MessageBox ("Hello from class");

Quote:
}

void CBoxesDlg::OnDebugCallfn()
{
  struct ContentFn  {
    int  index;
    void (CBoxesDlg::*call_back) (void);
  };

  ContentFn Select = { 1, TestFn };

  Select.call_back (); /* Error C2064: term does not evaluate */
                    /* to a function.  Error executing cl.exe */

Quote:
}

Thanks in advance.

Bob Sai

--



Mon, 14 Oct 2002 03:00:00 GMT  
 Function Pointer for Member Function


Quote:
> Hi,
> I have declared a function pointer for the member function of a class,
how
> do I call the function using the pointer?

> void CBoxesDlg::TestFn(void)
> {
>   MessageBox ("Hello from class");
> }

> void CBoxesDlg::OnDebugCallfn()
> {
>   struct ContentFn  {
>     int  index;
>     void (CBoxesDlg::*call_back) (void);
>   };

>   ContentFn Select = { 1, TestFn };

>   Select.call_back (); /* Error C2064: term does not evaluate */
>                     /* to a function.  Error executing cl.exe */
> }

This is a language question rather than an IDE
question, so if this reply is not enough, you
might consider asking in the language group.

Non-static member functions of classes have an
extra implied parameter, a "this", pointing at
the specific instance of the class they are to
work with.  To get a member function to work
as a callback, you have to somehow include that
information.  The syntax is a bit different,
and you need an instance.

If you make the member function static, then
you can do the stuff you'd have done with a
normal global function. But then the static
member function can only see static member data.

You can read more about this in the FAQ.
http://marshall-cline.home.att.net/cpp-faq-lite/

--
Dan Evens
Standard disclaimers etc. No spam please.



Tue, 15 Oct 2002 03:00:00 GMT  
 Function Pointer for Member Function
Your:
     Select.call_back

part of the statement
doesn't evaluate to a function, but a pointer-to-member function.

Try:

(this->*Select.call_back) ()

I don't know if this gets you where you were heading with this, but it works
for your example.

Quote:

>Hi,
>I have declared a function pointer for the member function of a class, how
>do I call the function using the pointer?

>void CBoxesDlg::TestFn(void)
>{
>  MessageBox ("Hello from class");
>}

>void CBoxesDlg::OnDebugCallfn()
>{
>  struct ContentFn  {
>    int  index;
>    void (CBoxesDlg::*call_back) (void);
>  };

>  ContentFn Select = { 1, TestFn };

>  Select.call_back (); /* Error C2064: term does not evaluate */
>                    /* to a function.  Error executing cl.exe */
>}

>Thanks in advance.

>Bob Sai

>--



Tue, 15 Oct 2002 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Function pointer for Member Function

2. Function Pointers to Member Functions

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

4. Pointers to member functions as function arguments

5. (member) function returning pointer to functions like itself?

6. Getting pointer to non-static member function from C callback function

7. function pointer to a member function

8. Function pointer / function member

9. How to pass a member function as inetrnal function pointer argument

10. member functions as function pointers!

11. function pointer to a member function

12. member function as function pointer

 

 
Powered by phpBB® Forum Software