I have the following scenario.
File Box.h
#pragma once
#include <functional>
class Box
{
public:
Box() :m_data(45) {}
void set_callback(std::function<int(int, int& c)> cb)
{
f = cb;
}
auto get_box_dimension(void)
{
return f(4,m_data);
}
private:
std::function<int(int, int&)> f;
int m_data;
};
File SquareBox.h
#pragma once
#include <functional>
class SquareBox
{
public:
int sq_func(int a, int& b)
{
return a * b;
}
private:
};
File main.cpp
#include<iostream>
#include<functional>
#include"Box.h"
#include"SquareBox.h"
int main()
{
int x = 1;
int a = 5;
Box b;
SquareBox sq_box;
b.set_callback(std::bind(&SquareBox::sq_func,&sq_box,a,x));
int ret_val = b.get_box_dimension();
std::cout << "Return Value == " << ret_val << std::endl;
return 0;
}
I was expecting the answer to be 5 times 45 . 45 being passed from the class Box. But the answer is 5. How can I make sure that the sq_func receives the value of second argument from class Box m_data ?