For now main problem is that you defined getValue()
as returning void
.
But let's skip that.
A function definition in C++ needs to be well defined. This means it needs to have immutable return type and argument list and a name. (There is couple of more attributes I believe but it is not that important here).
You can overload function so you have a couple of definitions with differing argument lists, however the returning type must be the same for all functions with the same name. You can get different returning type if you use templates, and the returning type would be a template argument.
Now to handle different types, there are two ways I believe. One is use templates and specializations.
You could define getValue()
as template<T> double getValue();
and then use different specializations to handle different branches of your original getValue
. In your example it would be:
//default case
template<typename T> double MyClass<T>::getValue() { return _y; }
//void case
template<> double MyClass<void>::getValue() { return _x; }
The second option is to use RTTI mechanism which allows to determine types of objects in run-time. The the code could like almost exactly like yours. E.g.
double getValue()
{
// if "Type == void" return _x, if "Type != void" return _y
return (typeid(Type) == typeid(void)) ? (_x) : (_y);
}
It all depends whether you can determine Type during compilation or not. The RTTI approach has its drawbacks. If you ever wanted to handle more types RTTI allows you to do it by modifying one function while template approach you would need to add another specialization. I guess its up to one's preferences which path to take... and template are quite nicer when it comes to design.
Edit: Oopsies ... I missed that your class is templatized by the Type
. So that should practically remove RTTI apporach out of question. I will leave the answer anyways if anyone ever comes here drawn by the sole title, as I believe it is still a valid way to do it.