#include <iostream>
class FunctionObject
{
voidoperator()(int y, int j);
};
int main()
{
void (*functorPointer)(int, int);
FunctionObject* functor = new FunctionObject();
functorPointer = functor;
return 0;
}
void FunctionObject::operator()(int y, int j)
{
using std::cout;
cout << "Function Pointer To Functor";
}
This currently fails to compile with a:
$ g++ functor.cpp
functor.cpp: In function 'int main()':
functor.cpp:15: error: cannot convert 'FunctionObject*' to 'void (*)(int, int)' in assignment
Is there any way to point a function pointer at an object with an overloaded operator() so that when I dereference the function pointer it would call that operator? Or would I have to create a pointer to member of type operator().
#include <iostream>
class FunctionObject
{
public:
voidoperator()(int y, int j);
};
int main()
{
void (FunctionObject::*functorPointer)(int, int);
functorPointer = &FunctionObject::operator();
FunctionObject* functor = new FunctionObject();
(functor->*functorPointer)(1, 3);
return 0;
}
void FunctionObject::operator()(int y, int j)
{
using std::cout;
cout << "Functor Pointer To Functor";
}