I’m writing a .hpp file for a class that should receive a function as one of it’s parameters and store it in a member variable, so far my code looks like this:
template<typename Function> class myClass
{
public:
myClass(Function function) : pvFunction( function ) {}
//Functor operator
double operator() (double x) const{
return pvFunction(x+4);
}
private:
Function pvFunction
}
The program looks pointless because it is, the values it returns are not important for now. I’m simply trying to figure out how to pass a function to this class and use its functor operator. The only problems are:
1) I don’t know if this class definition is correct, meaning is this the proper way of accepting any type of function to be passed as a parameter to an object of this class.
2) how do I create an instance of this class in my program? how do I pass the function to the new object and then call it?
Been at this for quite some time and can’t seem to figure it out
EDIT:
in my program file, main.cpp, this code receives an error:
double function(double);
int main()
{
myClass<double> myClassObject((function));
return 0;
}
double function(double x)
{
return (x+3.0);
}
1) Yes it is.
2) The Type of
myClassObjectfrom your example should besince function is of type
double(*)(double). The best way to construct objects of such a template, is to use a function template like this: