0

i have a header file with a class and several declaration and non-static member functions like this:

    //foo.h
    ...
    #include <bar.h>

    class foo
    {
        ...
        public:
            void myFunction();
    };

and a header file with a struct and a class like:

    //bar.h
    ...
    struct baz
    {
         class foo;
         ...
         void (foo::*functionPointer)() = NULL;
    }

    class bar
    {
        ...
        public:
            static myOtherFunction();
    };

and i want to address the function pointer in bar.cpp's static function "myOtherFunction" like:

    void bar::myOtherFunction()
    {
        ...
        baz b = baz();
        b.functionPointer = &foo::myFunction;
    }

but this gets me an compiler error:

    bar.cpp:247:22: error: cannot convert 'void (foo::*)()' to 'void baz::foo::*)()' in assignment
          b.functionPointer = &foo*:myFunction;
    Error compiling

Pretty Sure that this is an obvious fault for anyone experienced in c/c++ but for now i'm stuck and would be really grateful about some help. Thank you...

tobilocker
  • 891
  • 8
  • 27

1 Answers1

0

If you have access to C++11 libraries/classes in your compiler, include the functional library. Description here. It will show a way to make fully "packed" objects that have a reference to both the method you want, and the class instance.

If not, read the Pointers to Members section of the C++ FAQ very VERY closely. That's supported by everything everywhere as a core part of the language.

Good luck.

Kevin Anderson
  • 6,850
  • 4
  • 32
  • 54