Is it possible to assign a function from a specific instance of a class as a parameter? Multiple classes can have the same signature. Here an example. Thank you!
UPDATE: The context is that we are using a third party library that supply us with call back after a device finish the process. The typedef is what look like the signature from the third party library.
UPDATE: After checking the link, the solution seems to embed a specific class. Is there a way to be more generic?
typedef double(*Processor) (
double first,
double second
);
double Adjustment = 0.5;
double Multiply(double first, double second)
{
return first * second * Adjustment;
}
class Substract
{
public:
double Adjustment;
Substract(double adjustment) : Adjustment(adjustment) {}
double Process(double first, double second)
{
return first - second - Adjustment;
}
};
class Add
{
public:
double Adjustment;
Add(double adjustment) : Adjustment(adjustment) {}
double Process(double first, double second)
{
return first + second + Adjustment;
}
};
void Process(Processor processor)
{
double result = processor(100, 50);
std::cout << "Multiply=" << result << std::endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
double result;
Process(Multiply); // <-- success
Substract* substract = new Substract(0.5);
Process(substract->Process); // <-- not compiling
delete substract;
Add* add = new Add(0.5);
Process(add->Process); // <-- not compiling
delete add;
getchar();
}