Passing a function pointer of a member function to a non-member function

This is a discussion on Passing a function pointer of a member function to a non-member function within the C++ Programming forums, part of the General Programming Boards category; I've been having trouble with this section of code: Code: class classname { public: void functionname(void *exampledata) {} }; class ...

  1. #1
    C maniac
    Join Date
    Aug 2004
    Location
    Cyberwarping to Middle Earth
    Posts
    154

    Passing a function pointer of a member function to a non-member function

    I've been having trouble with this section of code:
    Code:
    class classname {
    public:
        void functionname(void *exampledata) {}
    };
    
    class class2 : public classname {
    public:
        void callfunctionpointer(void (*funcname)(void *)) {
            funcname(NULL);
        }
        void testfunctionpointer() {
            callfunctionpointer(functionname);
        }
    };
    It gives me an error. The only way I can get it to work is if I make functionname static.

    Anyone know how to help?

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    I'm not sure if it's this FAQ or one of the others on that page. All would make for good reading on the topic.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    C maniac
    Join Date
    Aug 2004
    Location
    Cyberwarping to Middle Earth
    Posts
    154
    It works perfectly, as the F.A.Q. says, as a global or static. I think it's this one that describes it best.

    I'll continue reading the F.A.Q.

    Thanks.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Taking a stab at unfamiliar territory...
    Code:
    #include<iostream>
    
    class base
    {
    public:
       void foo(void *data) { std::cout << "foo\n"; }
    };
    
    class derived : public base
    {
    public:
       void call(void (base::*function)(void *))
       {
          (*this.*function)(NULL);
       }
       void test()
       {
          call(&base::foo);
       }
    };
    
    int main()
    {
       derived d;
       d.test();
       return 0;
    }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing a pointer to a member function
    By Potterd64 in forum C++ Programming
    Replies: 1
    Last Post: 06-19-2006, 12:15 AM
  2. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 09:56 AM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  4. casting a void* to a pointer to a member function
    By Sebastiani in forum C++ Programming
    Replies: 13
    Last Post: 10-15-2002, 09:57 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM

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