I have an interface class MyFunction
. There are three functions in this class with the following signatures:
virtual bool Eval(int& iReturnVal, size_t szArgumentCount, list<Param> lParameterList) = 0;
virtual bool Eval(double& dReturnVal, size_t szArgumentCount, list<Param> lParameterList) = 0;
virtual bool Eval(char*& zReturnVal, size_t szArgumentCount, list<Param> lParameterList) = 0;
Now, any implementation of MyFunction
will only need to implement one of these functions depending on what type of value it needs to return. But I'll have to implement all 3 functions even if the other two functions are like this:
virtual bool Eval(double& dReturnVal, size_t szArgumentCount, list<Param> lParameterList){return false;}
which doesnt look so good. Or, I can declare all three functions like this in the interface:
virtual bool Eval(int& iReturnVal, size_t szArgumentCount, list<Param> lParameterList){return false;}
virtual bool Eval(double& dReturnVal, size_t szArgumentCount, list<Param> lParameterList){return false;}
virtual bool Eval(char*& zReturnVal, size_t szArgumentCount, list<Param> lParameterList){return false;}
Which also looks ugly. What is the less ugly of these two? Or is there a better way to do this?
EDIT:
On D Krueger's method :
#include <iostream>
using namespace std;
class Base
{
public:
template<typename T>
void F(T){cout << "Type T" << endl;}
};
class Imp : public Base
{
public:
template<int>
void F(int){cout << "Type int" << endl;}
};
int main(int argc, char** argv)
{
Base* pB;
Imp oI;
pB = &oI;
pB->F(1);
}
Looks like specialization does not apply across classes though derived. As template functions can't be virtual, this is a hopeless situation it seems.