I'm trying to create a non-template base class to create an interface through which I can interact with derived template classes. My hope is to use partial virtual function overloads, but something isn't working, and I'm not sure why. Can anyone explain why I the below code returns:
B match = False
D match = True
E match = False
instead of B,D returning True, and E returning False? I would have thought that the overloaded operator in the derived class would pick up the pointer-to-int '&i' and be called, but it doesn't.
To be clear, I'm not trying to -override- the Base version of match, I am trying to -overload- it, specifically wanting it to have a different, in this case more specialized, interface than the one in Base, which takes over when its function signature is used.
I'm also trying to avoid having to extend the Base class for every flavor of the Derived template that I might instantiate.
Even stranger, and I -might- just be going crazy here, but I swear this was working for me at some point not too long ago! Fwiw I'm on OSX 10.5, using llvm 7.0.0 and clang 700.1.76. Could this be a compiler idiosyncracy?
While I am attempting (unsuccessfully) to use partial overloads here, I'm really open to any approach that solves the problem of picking a template instance function by the type of its arguments, without proliferating classes, if-thens/case or adding specific specializations to the Base class. I'd love to hear it if you have another approach that I could drop in for similar functionality.
Thank you for any insights you can offer!
#include <stdio.h>
class Base
{
public:
Base() {}
virtual ~Base(){}
virtual bool match( const void *data ) const { return false; }
};
template <class Type>
class Derived: public Base
{
public:
Derived():Base() {}
~Derived() override{}
virtual bool match( const Type *data ) const { return true; }
};
int main(int argc, char **argv)
{
Derived<int> *d = new Derived<int>();
Derived<float> *e = new Derived<float>();
Base *b = d;
int i;
printf("B match = %s\n",b->match(&i)?"True":"False");
printf("D match = %s\n",d->match(&i)?"True":"False");
printf("E match = %s\n",e->match(&i)?"True":"False");
}