Well, I need it for my GUI system. I have a "Button" class so whenever it'll get clicked then it'll call a specific function. Though, I added some stuff to it and tested it, seems to be working:
#include <iostream>
#include <functional>
#include <string>
class Caller
{
public:
std::function<void()> func;
Caller();
void ExecFunc(std::function<void()> f);
void CallFunc();
void SetFunc(std::function<void()> f);
};
Caller::Caller() {}
void Caller::CallFunc()
{
ExecFunc(func);
}
void Caller::ExecFunc(std::function<void()> f)
{
f();
}
void Caller::SetFunc(std::function<void()> f)
{
func = f;
}
void test(std::string str)
{
std::cout << str << std::endl;
}
int main()
{
Caller caller1;
Caller caller2;
caller1.SetFunc([](){test("string");});
caller2.SetFunc([](){test("lol");});
caller1.CallFunc();
caller2.CallFunc();
}
That way, I can store a function to the class and call it later on. Though, will that cause some problems? Like memory leaks or anything?