I have a class called Game, and at init I pass a vector of GameObjects, but I'm also trying to pass callback functions that are stored in the class to be called later in the class's main Update loop.
How can I pass callback functions (that expect arguments), store it in the class and call them else where? Here's some pseudo code of what I'm talking about:
#include <iostream>
#include <string>
#include <functional>
class A
{
public:
A() = default;
~A() = default;
void BindCallback(std::function<int(int, int)> fn) {
privateCallback = std::bind(fn, std::placeholders::_1, std::placeholders::_2);
}
void MakeCallback(int i, int j) {
if (privateCallback)
std::cout << "Output from callback: " << privateCallback(i, j) << std::endl;
}
private:
std::function<int(int, int)> privateCallback; //Could do a vector of them, or whatever
};
int f(int a, int b) {
return (a + b);
}
int main()
{
A obj;
obj.BindCallback(f);
obj.MakeCallback(3, 4);
obj.MakeCallback(7, 8);
return 0;
}
#include <iostream>
#include <string>
class A
{
public:
A() = default;
~A() = default;
void BindOtherCallback(int(*fn)(int, int)) {
callback = fn;
}
void MakeOtherCallback(int i, int j) {
std::cout << "Ouput from callback: " << callback(i, j) << std::endl;
}
private:
int(*callback)(int, int);
};
int f(int a, int b) {
return (a + b);
}
int f2(int a, int b) {
return (a - b);
}
int main()
{
A obj;
obj.BindOtherCallback(f2);
obj.MakeOtherCallback(7, 2);
obj.MakeOtherCallback(9, -13);
obj.BindOtherCallback(f);
obj.MakeOtherCallback(7, 2);
obj.MakeOtherCallback(9, -13);
}
But I highly, highly recommend you use std::function instead. Is there a particular reason why you can't include a standard library to help with it? std::function
If you use regular C-style function pointers, you can't pass member functions around and access private members inside them (and other restrictions). std::function would be ideal except in some somewhat specific circumstances.
Thanks Jay, I'll give <functional> a try. I had no reasons not too, I'm just very paranoid with compile time and try to keep the number of libraries I include to a minimum. Again, I'm probably being unnecessarily paranoid.
Unless your program is time-critical, you shouldn't notice a difference between the amount of time it'll take to execute your callbacks using std::function and just using c-style function pointers. Compile times will go up, but not by much.
std::function adds a lot of functionality, well worth the extra little bit of time.