简体   繁体   中英

How to call a method of another object in C++?

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 !

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM