Hi all,
I've scoured the internet for examples that would allow me to figure out how to apply functors in my specific case, but I just can't seem to make the shift from the examples I find to figuring out how to use it for my own code.
Here is a reduced example of what I am currently facing. I have three files, that I am listing one by one here. The first file is Solver.h:
#ifndef SOLVER_H
#define SOLVER_H
// Include statements.
#include "MathematicalMethods.h"
// Forward declaration.
class MathematicalMethods;
class Solver
{
public:
Solver(){};
~Solver(){};
void setMathematicalMethod( MathematicalMethods* mathematicalMethod )
{
mathematicalMethod_ = mathematicalMethod;
};
void solve()
{
mathematicalMethod_->setDoubleTakingFunction( &Solver::computeValue, this );
mathematicalMethod_->execute();
};
double computeValue( double& value )
{
return 2.0 * value;
};
protected:
private:
MathematicalMethods* mathematicalMethod_;
};
#endif // SOLVER_H
The second file is MathematicalMethods.h:
#ifndef MATHEMATICALMETHODS_H
#define MATHEMATICALMETHODS_H
// Include statements.
#include <iostream>
#include "Solver.h"
// Forward declarations.
class Solver;
class MathematicalMethods
{
public:
typedef double ( Solver::*pointerToDoubleTakingFunction ) ( double& );
MathematicalMethods(){};
~MathematicalMethods(){};
void setDoubleTakingFunction( pointerToDoubleTakingFunction doubleTakingFunction,
Solver* pointerToSolver )
{
pointerToDoubleTakingFunction_ = doubleTakingFunction;
pointerToSolver_ = pointerToSolver;
};
void setValue( const double& value )
{
value_ = value;
};
void execute()
{
std::cout << ( pointerToSolver_->*pointerToDoubleTakingFunction_ )( value_ ) << std::endl;
};
protected:
pointerToDoubleTakingFunction pointerToDoubleTakingFunction_;
Solver* pointerToSolver_;
double value_;
private:
};
#endif // MATHEMATICALMETHODS_H
The last file contains the main function:
// Include statements.
#include "Solver.h"
#include "MathematicalMethods.h"
int main()
{
MathematicalMethods* pointerToMathematicalMethod = new MathematicalMethods;
pointerToMathematicalMethod->setValue( 10.0 );
Solver mySolver;
mySolver.setMathematicalMethod( pointerToMathematicalMethod );
mySolver.solve();
return 0;
}
This works fine for me and produces the intended output of 20.0 but there are a number of problems. Firstly, the code is not easily extensible to add other solvers that make use of the MathematicalMethods class. Secondly, in the actual implementation that I have, both Solver and MathematicalMethods belong to inheritance trees, which means that the circular class declarations become quite complicated, and for some reason my compiler at time just refuses to compile the code. In other words, the code doesn't seem to be stable at all and I can't seem to figure out how to stabilize it.
My understanding about functors is that they make this whole task a whole lot easier. Additionally, if I understand the examples that I've read correctly, the use of functors would also make it a trivial task to add additional solvers that make use of the MathematicalMethods class.
I'm generally quite good at reading tutorials and transcribing them to my application, but for some reason I'm having a brain freeze and I can't see how I should go about rewriting this code to make use of functors instead of the pointer-to-member function solution that I've got now.
I hope you someone here can give me a headstart so that I can get this sorted out asap.
My apologies if I've posed the question in a contorted manner or if the solution ends up being a straightfoward application of basic functor examples, I just don't see at the moment how to go about it.
Thanks in advance for your help!
Cheers,
Kartik