I tried to bind a classes method from another class which is storing the first one's pointer, but it always gives me different value. What am I doing wrong?
If I pass class A by value (and of course modify class B to store by value) it's working.
#include <iostream>
#include <functional>
using namespace std;
class A {
public:
A(double a, double b) : a(a), b(b) {}
double mul(void) {return a*b;}
private:
double a;
double b;
};
class B {
typedef std::function<double(void)> function;
public:
B(A* ap) : ap(ap) {}
function a_mul = bind(&A::mul, ap);
private:
A* ap;
};
int main() {
A* a = new A(2,3);
B b(a);
cout << b.a_mul() << endl;
return 0;
}