I am using a pattern in my program that calls for the calling of different kinds of functors depending on what the object type is. In the code below, the b1
object is explicitly instantiated with derived functor type FunctorX
. But when I make the call in the code, the base functor gets called instead of the derived functor. Why is that?
struct Functor
{
Functor() {}
~Functor() {}
virtual int operator()() { return 33; }
};
struct FunctorX : public Functor
{
FunctorX() : Functor() {}
~FunctorX() {}
int operator()() { return 44; }
};
template< typename FunctorT >
struct B
{
B( FunctorT FunctorArg ) : Functor { FunctorArg } {}
~B() {}
FunctorT Functor;
};
int main()
{
B< Functor > b1 { FunctorX() }; // initialize with derived functor. Derived functor gets instantiated.
int num = b1.Functor(); // error: calls base functor, not derived functor
return 0;
}