How to Pass a Function to Structure in C++? - c++

My Struct Code :
struct MouseHandler
{
void OnHover();
void* OnLeftClick();
// void (*OnLeftClick)();
void OnRightClick();
void OnDrag();
void OnDrop();
};
Calling of Struct :
MouseHandler ms;
I am trying to pass SampleScrollDownClickHandler to ms.OnRightClick(), Is it is possible, How can I do that, I am tried this, but I failed :
ms.OnRightClick() = SampleScrollDownClickHandler();
I am not using std library!

So the commented out line is actually closer to the solution you want. In order to pass the function pointer you need to not call the function itself, which you did by invoking it with the trailing (). So the following code is closer to what you want (limiting it just to the most basic parts of your code):
struct MouseHandler {
void (*OnLeftClick)();
void OnRightClick();
void OnDrag();
void OnDrop();
};
void my_callback() {
}
int main() {
MouseHandler m;
m.OnLeftClick = my_callback;
}

The member variables of the struct are not defined correctly.
void OnHover(); declares a member function, not a member variable. You need to use void (*onHover)();, which can point to a non-member function.
struct MouseHandler
{
void (*OnHover)();
void (*OnLeftClick)();
void (*OnRightClick)();
void (*OnDrag)();
void (*OnDrop)();
};
With that change, you can use:
MouseHandler me;
me.OnRightClick = SampleScrollDownClickHandler;
as long as SampleScrollDownClickHandler is declared and is the right function type.

Related

Passing non-static member function as argument to a member function in a different class

UPDATE I realize this question's lacking a proper MCVE, it will take me some time to come up with one. I will update it when I have time to come back to this, sorry. I appreciate the answers thus far.
Following this answer regarding static functions:
Declaration (in MyClass)
void MyClass::func ( void (MyOtherClass::*f)(int) ); //Use of undeclared identifier 'MyOtherClass'
Example of function being passed to func:
void MyOtherClass::print ( int x ) {
printf("%d\n", x);
}
Function call (in MyOtherClass)
void MyOtherClass::loop(){
func(&MyOtherClass::print);
}
How can one pass a member function as a parameter of a member function of another class?
According to the ISO, the answer is "don't". Unlike normal functions, a non-static member function is meaningless without an instance of the class. As a workaround, you can have your calling function take a std::function and pass it a lambda.
Example:
void calling_func(std::function<void()> f);
struct foo
{
void func();
void call()
{
calling_func([this]{
func();
});
}
};
can't you just use std::function and std::bind to that?
class MyOtherClass
{
public:
MyOtherClass() {}
void print(int x)
{
printf("%d\n", x);
}
};
class MyClass
{
private:
std::function<void()> CallbackFunc;
public:
MyClass() {};
void AssignFunction(std::function<void(int)> callback, int val)
{
CallbackFunc = std::bind(callback, val); //bind it again so that callback function gets the integer.
}
void DoCallback()
{
CallbackFunc(); //we can then just call the callback .this will, call myOtherClass::print(4)
}
};
int main()
{
MyClass myObject;
MyOtherClass myOtherObject;
int printval = 4;
//assign the myObject.callbackfunc with the myOtherClass::print()
myObject.AssignFunction(std::bind(&MyOtherClass::print, myOtherObject,std::placeholders::_1), 4);
//calling the doCallback. which calls the assigned function.
myObject.DoCallback();
return 0;
}

Passing member function to another object's member function C++

I am having issues trying to pass a function as an argument in another object's function. I am well aware there are many similar topics but I either can't get their solution to work or can't understand them.
class foo
{
public:
void func1(void (*Drawing)(void));
template<class T>
void func2(void (T::*Drawing)(void));
};
class bar
{
private:
foo myFoo;
void Drawing();
void func3() {
// Attempt 1
myFoo.func1(Drawing);
// Attempt 2
myFoo.func2<bar>(&bar::Drawing);
}
};
So in my first attempt, I get the error where you can't convert void (bar::*)(void) to void (*)(void) of which I then found out there are normal function pointers and member function pointers.
Attempt 2 was my feeble attempt to overcome this but I get unresolved externals now...
So how can I successfully pass my Drawing() member function into another function from another object?
The issue is that you cannot consider bar::Drawing as a void (*)(void) function since it's a non static method, which therefore required an object (the this context which will be used)
A solution, assuming c++11 is ok for you, would be to use std::bind and to sligtly modify your foo definition:
class foo
{
public:
void func1(std::function<void(void)> Drawing)
{
Drawing(); // or do whatever you want with it
}
};
Then you will be able to do
void bar::func3() {
myFoo.func1(std::bind(&bar::Drawing, this));
}
making valid a lot of potential uses
int main()
{
bar myBar;
myBar.func3();
foo myFoo;
myFoo.func1([](){ printf("test\n"); });
return 0;
}
I'm guessing you've left out important details as to what you're trying to accomplish. However, the following should give you some idea of what you need to do.
Code
#include <iostream>
class foo
{
public:
void func1(void (*Drawing)(void))
{
std::cout << "func1\n";
}
template<class T>
void func2(T* instance, void (T::*fn)(void))
{
std::cout << "func2: ";
(instance->*fn)();
}
};
class bar
{
public:
bar()
{
func3();
}
private:
foo myFoo;
void Drawing()
{
std::cout << "bar::Drawing()\n";
}
void func3()
{
// Attempt 1
//myFoo.func1(Drawing); // This won't work
// Attempt 2
myFoo.func2(this, &bar::Drawing); // Must pass an object instance if you plan to use the member function
}
};
int main(int argc, char *argv[])
{
bar b;
return 0;
}
Sample Output
func2: bar::Drawing()

Passing a void function as an argument to another function

I'm trying to pass a void function to another void function, unsuccessfully so far. So I created this function inside the class called ExitButton like this. ExitButton.h:
class ExitButton{
void setup(void (*_setup));
};
Then I include that class into another class like this. ofApp.h:
include "ExitButton.h"
class ofApp : public ofBaseApp{
void update();
void setup();
StartButton *startButton;
}
So in my ofApp.cpp I want to call the update function like this:
void ofApp::update(){
exitButton->setup(setup()); // This throws me the following error: Cannot initialize a parameter of type 'void (*)' with an rvalue of type void
}
So I assume, I can only pass a void function that is a pointer?
Is it actually possible to pass a void function as a parameter to another function?
This is probably what you want:
#include <iostream>
using namespace std;
class ExitButton{
public:
void setup(void (*_setup)())
{
_setup(); // we call the function pointer
};
};
void setup() // this is a void function
{
cout << "calling void setup()" << endl;
}
int main()
{
ExitButton eb;
eb.setup(setup); // use a void function as a parameter
}

Passing a function pointer to a function

What should I do if I want to pass a non-static member function of any class as a click function of the button ? Is it possible ? If so what do I need to do ? For example in which ever class (EntityToolGUI over here) the button is initiatlized, I want to set its click action to a non-static member function (a non-static member function of class EntityToolGUI ) of that class.
GUIButton.h
typedef void (*ptr2clickFunc)(void);
class GUIButton : public GUIObject {
private : void (*clickFunc)(void);
public : void setClickFunction(ptr2clickFunc clickFunc);
};
GUIButton.cpp
void GUIButton::setClickFunction(ptr2clickFunc clickFunc)
{
this->clickFunc = clickFunc;
}
EntityToolGUI.h
class EntityToolGUI {
public : EntityToolGUI();
protected : void addAnimation();
}
EntityToolGUI.cpp
void EntityToolGUI::addAnimation()
{
cout<<"add animation"<<endl;
}
EntityToolGUI::EntityToolGUI()
{
....
btnAddAnimation->setClickFunction(&EntityToolGUI::addAnimation);
}
I am getting an error no matching function call to GUIButton::setClickFunction(void (EntityToolGUI::*)())
candidate is void GUIButton::setClickFunction(void (*)())
How do I solve this ?
Most (decent) C code that passes function pointers around use an extra void* argument for passing user context to the function. This is not so common in C++ (as better techniques than function pointers exist), but if you're stuck using function pointers for some reason then it may be appropriate.
typedef void (*ptr2clickFunc)(void*);
class GUIButton : public GUIObject {
private : ptr2clickFunc clickFunc;
private : void * userdata;
public : void setClickFunction(ptr2clickFunc clickFunc, void* userdata);
};
class Foo
{
static void do_foo( void * userdata )
{
Foo* thisptr = static_cast<Foo*>(userdata);
thisptr->foo();
}
void foo() { ... }
};
int main()
{
Foo foo;
GUIButton button;
button.setClickFunction( &Foo::do_foo, &foo );
button.click();
}
EDIT As noted by Bartek, if you're doing this a lot you can extract the static function into a template - it looks a bit like this (untested and probably with minor errrors).
// GUIButton is as before
// Note no static function here
class Foo { void foo(); }
template<typename T, void(T::*FN)() >
void Call( void * data)
{
static_cast<T*>(data)->*FN();
}
int main()
{
Foo f;
GUIButton button;
button.setClickFunction( &Call<Foo,&Foo::foo>, &f );
button.click();
}
If you want to pass obj fun ptr you can use boost::bind and boost::function
http://www.boost.org/doc/libs/1_50_0/libs/bind/bind.html
You cannot pass a pointer to non-static member function as a pointer to a "regular" non-member function. You should either make addAnimation static, or make ptr2clickFunc typedef a pointer to member function.
Note that invoking a pointer to member function is different from invoking a function pointer, because you must supply an instance on which the member pointer is to be invoked.
addAnimation needs to be static function. When the call back function is set the way you are doing it now, the object of class EntityTollGUI is not registered along with the function.
Try this one (C++11):
#include <stdio.h>
#include <stdlib.h>
#include <functional>
class Raiser
{
public:
std::function<void(int)> ev1, ev2;
void RaiseEv1()
{
if (!ev1._Empty())
ev1(44);
}
void RaiseEv2()
{
if (!ev2._Empty())
ev2(66);
}
};
class Handler
{
private:
int id;
std::function<void(int)> h;
public:
Handler(int newId)
{
id = newId;
h = [this](int i)
{
printf("Handler with id = %d captured event!\n", this->GetId());
};
}
void Hook1(Raiser & raiser)
{
raiser.ev1 = h;
}
void Hook2(Raiser & raiser)
{
raiser.ev2 = h;
}
int GetId()
{
return id;
}
};
int main(int argc, char * argv[])
{
Raiser raiser;
Handler handler1(1), handler2(2);
handler1.Hook1(raiser);
handler2.Hook2(raiser);
raiser.RaiseEv1();
raiser.RaiseEv2();
getchar();
}
AFAIK, this is the most you can get with events in C++ without using language extensions.

How to pass a method as callback to another class?

I have a question regarding callbacks using tr1::function. I've defined the following:
class SomeClass {
public:
typedef std::tr1::function<void(unsigned char*, int)> Callback;
void registerCallback(Callback);
private:
Callback callback;
}
I've defined another class:
class SomeOtherClass {
void myCallback(unsigned char*, int);
}
Now I want to register my function 'myCallback' as callback at class 'SomeClass'using the method 'registerCallback'. However, it is not working. I've had a look on the boost documentation on the function and it seems legit to use (member) methods of a class for callbacks. Am I wrong?
Thanks in advance!
Member functions have an implicit first parameter, a this pointer so as to know which object to call the function on. Normally, it's hidden from you, but to bind a member function to std::function, you need to explicitly provide the class type in template parameter.
#include <functional>
#include <iostream>
struct Callback_t {
void myCallback(int)
{
std::cout << "You called me?";
}
};
class SomeClass {
public:
SomeClass() : callback() { }
typedef std::function<void(Callback_t*, int)> Callback;
// ^^^^^^^^^^^
void registerCallback(const Callback& c)
{
callback = c;
}
void callOn(Callback_t* p)
{
callback(p, 42);
}
private:
Callback callback;
};
int main()
{
SomeClass sc;
sc.registerCallback(&Callback_t::myCallback);
Callback_t cb; // we need an instance of Callback_t to call a member on
sc.callOn(&cb);
}
Output: You called me?;
Why all this complicated mumbo-jumbo?
Why not create a class as thus (for example)
Class MouseOverEventCallBack
{
public:
virtual void RunMouseOverCallback() = 0;
};
Then just create classes that inherit this class (and redefine the method RunMouseOverCallback)
Then Register function just needs to be
void registerCallback(MouseOverEventCallBack *callbackObject); // possible could use a reference
The register method will just call the method and the object will have all that it needs.
Seems a bit simpler. Let the compiler do the work with pointers to functions etc.
the function void (*)(unsigned char*, int) is a free function, which is a different type from void (SomeOtherClass::*)(unsigned char*, int), thus the error. You need an object to call the latter, while the former is a free function.
Look at the possible solutions listed in the Boost documentation
Another possibility is that your SomeOtherClass::myCallback is private, so you do not have access to it.
Use templates:
template <class T>
class B
{
public:
typedef void (T::*TCallBackFunction)(void);
void SetCallBack(T* pCallBackClass, TCallBackFunction pCallBackFunction)
{
if(pCallBackFunction && pCallBackClass)
{
m_pCallBackFunction = pCallBackFunction;
m_pCallBackClass = pCallBackClass;
}
}
void StartCallBackFunction()
{
(pCallBackClass->(*m_pCallBackFunction))();
}
private:
TCallBackFunction m_pCallBackFunction;
T* m_pCallBackClass;
};
Such like this. And use it:
...
B<MyClass> b;
b.SetCallBack(&b, &MyClass::MyFunction);
...