Say I have these 3 different functions that I may want to point to:
float test1(int a, int b) {
return 7.0f;
}
struct TestClass {
float test2(int a, int b) {
return 5.0f;
}
};
struct TestClass2 {
float test3(int a, int b) {
return 3.0f;
}
};
Notice how all three use the same arguments and return value. I want to abstract away whether or not it is a member function and which class it belonged to. I want a delegate type that could be referring to any of these 3 functions depending only on how it was initialized.
Here is my first attempt:
typedef std::function<float(int, int)> MyDelegate; // is this right?
int main() {
TestClass obj;
TestClass2 obj2;
MyDelegate a = test1;
MyDelegate b = std::bind(std::mem_fn(&TestClass::test2), obj); // is this right?
MyDelegate c = std::bind(std::mem_fn(&TestClass2::test3), obj2); // is this right?
return 0;
}
The idea is I want to also store the 'this' pointer inside the wrapper too. This way, it's like a fully functional delegate.For example, invoking 'b(x, y)' should be like calling obj.test2(x, y)
.
I just can't even make it compile, I'm probably not fully grasping this. I'm kind of new to these libraries and the errors in VS2012 are catastrophically unhelpful. Any help would be appreciated.