Say I have a class like this:
Code:
class Ta {
private:
// some class-local vars here
public:
void *GetFromStore (int bucket, int id);
// other functions
}
This is the base class for several other classes that need to store data in this type of way. However, each other class uses a different type of pointer, and so has different return types.
Instead of using casting throughout the program, I overload the function and change the return type:
Code:
class Tb : Ta {
public:
char *GetFromStore (int bucket, int id);
}
The class is then used as follows:
Code:
Tb cvar;
char *p;
p = cvar.GetFromStore (0, 1);
Where the function GetFromStore would be defined in .cpp file as:
Code:
char * Tb::GetFromStore (int bucket, int id)
{
return ((char *)Ta::GetFromStore (bucket, id));
}
Now, is this the best of dealing with it? I'm suspect not, and think there must be an easier/better way of doing this or calling the previous function (before the overloaded function took over it's name).
Looking through a few of my C++ books simply says that overloading and just changing the return type is invalid and can't be done, but the above compiles ok with no errors or warnings. If I place code in the Ta class to return a value, then the main code received that value, so the compiler is obviously interpreting it correctly, but it just seems so ugly that there must be a better way of doing this.