Implement a Callback to a static C++ Member Function ?

This is a discussion on Implement a Callback to a static C++ Member Function ? within the C++ Programming forums, part of the General Programming Boards category; Hi everyone, it's hard days to understand deeper about function pointer, now i get trouble with that, i need your ...

  1. #1
    Registered User PuKa's Avatar
    Join Date
    Oct 2011
    Posts
    23

    Implement a Callback to a static C++ Member Function ?

    Hi everyone, it's hard days to understand deeper about function pointer, now i get trouble with that, i need your help, and i very pleasure to know what's wrong i've made. thanks a lot.

    Code:
    #include <iostream>
    using namespace std;
    class one {
    public:
        static void show() { cout << "Hi" << endl; }
    };
    int main() {
        static void (one::*f)();
        f = &one::show;
        one oo;
        (oo.*f)();
        return 0;
    }
    //output:
    //1.cc: In function ‘int main()’:
    //1.cc:9: error: cannot convert ‘void (*)()’ to ‘void (one::*)()’ in assignment

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    2,588
    when you declare f, it should just be:

    Code:
    void (*f)();

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    What you have created is a pointer to a non-static member function. Just FYI.
    Oh, and, to add to Elkvis: when you want to call the function, you just do f(), not (oo.*f)(). This is a static member, after all, so it does not "belong" to any instance.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User PuKa's Avatar
    Join Date
    Oct 2011
    Posts
    23
    Thanks so much for your helps. Elysia and Elkvis.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Callback into a class member function
    By kovacsbv in forum C++ Programming
    Replies: 4
    Last Post: 05-23-2009, 11:06 AM
  2. Ptr-to-member as callback in a c-function
    By pheres in forum C++ Programming
    Replies: 1
    Last Post: 06-26-2007, 01:41 AM
  3. Member Function Callback - No User Supplied Args
    By Tonto in forum C++ Programming
    Replies: 3
    Last Post: 01-01-2007, 10:26 PM
  4. Is it possible to have callback function as a class member?
    By Aidman in forum Windows Programming
    Replies: 11
    Last Post: 08-01-2003, 12:45 PM
  5. Pointer to member function as callback function
    By ninebit in forum C++ Programming
    Replies: 10
    Last Post: 03-01-2002, 05:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21