Massimo said:
Oops... you are right, it's function overloading... :-|
So it's better to create an abstract base class and derive two other
classes from it in this situation? Any client of this class will manage
only one type of reference, so it calls only the function returning the
type it needs.
It all depends on your needs. There is no one answer to your question.
That said, I work daily on code that used overloading instead of
polymorphism in a "dispatch" system. This means that yes, all clients
have to know what type they are working with and worst, you can never
use the generic type as the compiler won't know what to do with it; if
you have a pointer to the generic you have to establish what subclass
it is and cast it to a pointer of that type in order to call the
function. In this case there are some times when you need to do
something different with one type or the other (code breaks LSP
bigtime) but IMHO it should have been done in a function expecting the
generic type (it should have established if this was a special needs
type and put it where it wanted) or at the least only override in the
case when something different need be done; I expect someone though
maybe something different would be done down the road and so just
overrode for everything...you should see the switch statement during
the file read that does something like:
switch (readType)
case BLH_TYPE:
thingy->Dispatch(new BlhType(data)); // parsing constructor
break;
case BLF_TYPE:
thingy->Dispatch(new BlfType(data));
break;
....
Where I come from we call that fugly and it has a very bad odor (of
course the whole overloading thing is just one smell in this case, but
it is a bad one).