C++ Pass a member function to another class

Axel

I'm trying to make a simple callback in C++ but I'm having issues doing it as I want to.

Essentially I want something like this:

class A{
   void A::AddCallback(void (*callBackFunction)(string)){
      /*Code goes here*/
   }
}

And class B

#include "A.h"
class B{
   B::B(){
      A childObject;
      childObject(MyCallBackFunction);   
   }
   B::MyCallBackFunction(string arg){
      /*Code goes here*/
   }
}

I know that usually you would want to define the header of AddCallback with something like B::callBackFunction but I do need to import A in B so I it would be awkward to have both classes import each other. I know I've seen this before but I cant get the syntax right

Kerrek SB

Here is one option using a static member function:

#include <string>

struct A
{
    void AddCallback(void (*cb)(std::string));
};

struct B
{
    A a;

    B() { a.AddCallback(&B::MyFun); }

    static void MyFun(std::string);
};

If you want a non-static member function, then you first need to decide on which instance of B you want the member function to be invoked. For example, to invoke it on the object whose constructor registers the callback:

#include <functional>
#include <string>

struct A
{
    void AddCallback(std::function<void(std::string)>);
};

struct B
{
    A a;

    B() { a.AddCallback(std::bind(&B::MyFun, this, std::placeholders::_1)); }

    void MyFun(std::string);
};

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

C++ How to pass member function pointer to another class?

From Dev

call member function in another class c++

From Dev

call member function in another class c++

From Dev

C++: How do I pass a pointer to a member function of another class?

From Dev

Calling another member function from a function in a class template in C++

From Dev

Why constructor is not being called in member function of another class in C++?

From Dev

Cannot pass class member-function to another function(std::thread::thread)

From Dev

Member function of a class as friend to another class

From Dev

Class design to call member function of another class

From Dev

how to pass a member function with args as an argument to another member function?

From Dev

how to pass pointer to member function of a template class?

From Dev

Pass Member Function Pointers to parent class

From Dev

Calling member function of one class from another

From Dev

Declare and use a pointer to member function in another class

From Dev

Invalid use of non-static member function - Class Member Function Calling Another Class Member Function

From Dev

How do i get a class member function to have access to another class member function's private member?

From Dev

another initialize a static c++ class member

From Dev

Avoid the class scope so as to pass a member function as a function pointer

From Dev

calling method in template class in template member function of another class

From Dev

Why can't a PRIVATE member function be a friend function of another class?

From Dev

Passing member function address to another class`s function

From Dev

How do I call a function that is member of a class, from another function?

From Dev

Setting a value by a friend function which is a member function of another class

From Dev

How to pass a function to another class using IoC

From Dev

C++11 multithreading with class member function

From Dev

Specialization of member function of a template class in C++

From Dev

C++ Class with Rcpp::Function member

From Dev

C++ vector as class member vs in function

From Dev

c++ class passing member function issues

Related Related

  1. 1

    C++ How to pass member function pointer to another class?

  2. 2

    call member function in another class c++

  3. 3

    call member function in another class c++

  4. 4

    C++: How do I pass a pointer to a member function of another class?

  5. 5

    Calling another member function from a function in a class template in C++

  6. 6

    Why constructor is not being called in member function of another class in C++?

  7. 7

    Cannot pass class member-function to another function(std::thread::thread)

  8. 8

    Member function of a class as friend to another class

  9. 9

    Class design to call member function of another class

  10. 10

    how to pass a member function with args as an argument to another member function?

  11. 11

    how to pass pointer to member function of a template class?

  12. 12

    Pass Member Function Pointers to parent class

  13. 13

    Calling member function of one class from another

  14. 14

    Declare and use a pointer to member function in another class

  15. 15

    Invalid use of non-static member function - Class Member Function Calling Another Class Member Function

  16. 16

    How do i get a class member function to have access to another class member function's private member?

  17. 17

    another initialize a static c++ class member

  18. 18

    Avoid the class scope so as to pass a member function as a function pointer

  19. 19

    calling method in template class in template member function of another class

  20. 20

    Why can't a PRIVATE member function be a friend function of another class?

  21. 21

    Passing member function address to another class`s function

  22. 22

    How do I call a function that is member of a class, from another function?

  23. 23

    Setting a value by a friend function which is a member function of another class

  24. 24

    How to pass a function to another class using IoC

  25. 25

    C++11 multithreading with class member function

  26. 26

    Specialization of member function of a template class in C++

  27. 27

    C++ Class with Rcpp::Function member

  28. 28

    C++ vector as class member vs in function

  29. 29

    c++ class passing member function issues

HotTag

Archive