Click here to Skip to main content
15,888,039 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hallo community, i have currently this code in C++. I want to pass the reference of a void method so it can be called from another method in another class.

C++
//main.cpp
#include "A"
void Handler(int val);
void Handler(int val) 
{
  printf("%d", val); //should display 5
}
int main()
{
   A fnc;
   fnc.execute(&Handler); //no matching function for call to (void (::*)(int))|
}
//A.h
public:
void execute(void **something(int val));

//A.cpp
void execute(void **something(int val))
{
   //call Handler()
   something(5);
}


What I have tried:

-------------------------------------------------------
Posted
Updated 17-Jun-17 13:40pm
v6
Comments
Richard MacCutchan 18-Jun-17 2:39am    
Too many levels of indirection.

1 solution

Using this technique in C++ is named callbacks.

Here you find a tutorial about callbacks.

As an experienced programmer like interfaces more, because type checking from the compiler, debugging adavantages and cleaner concepts like modularity and maintenance. Here you find some tutorial about interfaces.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900