I have a simple template class that has two function pointers and a value. One pointer is a comparison operator and the other is of an object (for instance getX()).
Header:
template<class T>
class A {
public:
A(T (*function)(), const bool (*op)(T,T),const T value) :
function(function), value(value), op(op){}
bool valid();
private:
T value;
T (*function)();
bool (*op)(T, T);
};
CPP:
#include "A.h"
template<class T>
bool A<T>::valid(){
return (op(function(),value));
}
So if you created an instance of A:
A<float> a = A<float>(x->getX,operator==,20);
When valid is called is would be equivalent to:
x->getX() == 20;
The actual header/class definitions work fine. The problem is when creating an instance of A; it doesn't work. I figure this is because of the 'x->getX' but is there a way to do what I want?
Thanks.
EDIT::
The exact compiler error is the following:
....\Classes\Objects\B.cpp:42: error: no matching function for call to 'A::A(unresolved overloaded function type, unresolved overloaded function type, float)' ....\Classes\Objects/A.h:30: note: candidates are: A::A(T ()(), const bool ()(T, T), T) [with T = float] ....\Classes\Objects/A.h:26: note: A::A(const A&)
Please note: the 'x->getX' returns a float
operator(). - Joseph MansfieldgetXis a method of some class.xis an object of that class. You can't passx.getXas a pointer to normal function. It doesn't work in C++, like delegates in C#. The easiest way I can think of, to get the result you want is to make a wrapper. There must be some idiomatic way of doing it in C++, but you need someone who knows the language to tell you about it. - Maciej Hehl