2

I define a function pointer type:

typedef void(*Callback)(void);

I have a public method in a class :

class HasMethod{
    public:
        void MyMethod(){} // do ....
};

And a difference class has a function pointer:

class HasFunctionPointer{
    public:
        Callback CallbackPointer = 0; // default
}

And create two objects from classes. And I want as below:

int main(){
    HasMethod objmethod;
    HasFunctionPointer objfpointer;

    // I want use function pointer of objfpointer to call method of objmethod.
    // When I call :       objfpointer.CallbackPointer();
    // objmethod.MyMethod will run.
    // As delegate in C#.
}

Thanks for watching ! Please help me !

  • 1
    Possible duplicate of [Callback functions in c++](https://stackoverflow.com/questions/2298242/callback-functions-in-c) – Sneftel Mar 31 '18 at 09:49
  • If you have more than one `HasMethod` object, how does `objfpointer.CallbackPointer()` know which object is called? – xskxzr Mar 31 '18 at 09:50
  • 8
    A pointer to a non-member function is *not* the same as a pointer to a member function. Pointers to non-member function don't need an object to be called on, while pointers to member function do need that. I suggest you read more about [`std::function`](http://en.cppreference.com/w/cpp/utility/functional/function), [`std::bind`](http://en.cppreference.com/w/cpp/utility/functional/bind) and [lambdas](http://en.cppreference.com/w/cpp/language/lambda). – Some programmer dude Mar 31 '18 at 09:52
  • Hi @xskxzr ! In C#, I do likes this : objfpointer.Callback = objmethod.MyMethod; – Văn Dương Nguyễn Mar 31 '18 at 09:56
  • Thank you for your suggestion ! @Someprogrammerdude. – Văn Dương Nguyễn Mar 31 '18 at 10:01
  • Related and possible duplicate: https://stackoverflow.com/q/49425281/5376789 – xskxzr Mar 31 '18 at 10:26
  • Thank my guys ! Thank you @Someprogrammerdude ! I'm using std::bin and it work now :) – Văn Dương Nguyễn Mar 31 '18 at 11:21

1 Answers1

0

There is some explanation, then some alternative code that might help in your situation at the bottom of the post, I would recommend going with a simpler solution, but one of the beauties of C++ is that you can do almost anything. So:

In C++, a class method has a hidden first parameter of this pointer, so that the method can easily operate on the calling object. A method pointer would have to be able to know which object to call the method on, which kind of makes method pointers a bit tricky. That makes it so that a normal function pointer doesn't have the same signature as a member method that accepts the same argument list, because the first argument of the class method is automatically the this pointer (static methods don't have the this pointer, so they are different in that regard).

I think your question is roughly equivalent to this one: C++: Function pointer to another class function

The suggestion there is to use a static method which accepts an object pointer so that it can call the object correctly; it is much easier to work with that static method. But you still need to know a specific object that you are calling on.

For an example of doing class method pointers, this question answers that quite nicely, but you still have to dereference the this object to know which class to call the pointer on, which usually happens inside the same class (using pointer to decide which class member to call from within the same class): Calling C++ class methods via a function pointer

Solution using both Class and Method Pointers

If you really want to call a method of an object from another class, then you will need pointers to both the object you are calling the method on AND the method you would like to call. That way you can switch the pointer around to different objects and switch the method pointer to different methods. I am having a hard time envisioning a use case for this (except some really fringe ones), but I had fun putting it together so--- One implementation is below:

#include <iostream>

typedef void(*Callback)(void);

class HasMethod{
public:
    void MyMethod(){ std::cout << "Method run on object"; } // do ....
};

typedef void (HasMethod::*MethodCallback)(void);

class HasFunctionPointer{
public:
    Callback CallbackPointer = 0; // default
    HasMethod* classToCall = nullptr;
    MethodCallback CallbackPointer2 = 0; // default
};


int main()
{
    HasMethod objmethod;
    HasFunctionPointer objfpointer;

    objfpointer.CallbackPointer2 = &HasMethod::MyMethod;
    objfpointer.classToCall = &objmethod;
    HasMethod* classObject = objfpointer.classToCall;
    MethodCallback intermed = objfpointer.CallbackPointer2;

    (classObject->*intermed)();

    return 0;
}
Halcyon
  • 1,376
  • 1
  • 15
  • 22