I want to override a function with same parameter type, but with different logical meaning. I’ve tried something like:
class T
{
};
typedef T Age;
typedef T Height;
typedef T Weight;
void foo(Age a){}
void foo(Height a){}
void foo(Weight a){}
but I have build errors:
error C2084: function ‘void foo(Age)’ already has a body
One solution would be:
class Age : public T{};
class Height : public T{};
class Weight : public T{};
but I don’t want to fill my namespace with new classes only for this purpose.
How may I achieve this without using derived classes?
EDIT: My code is in a cpp file, I don’t use headers. This is just a simple example. Full cpp content is here:
class T
{
};
typedef T Age;
typedef T Height;
typedef T Weight;
void foo(Age a){}
void foo(Height a){}
void foo(Weight a){}
int main()
{
return 0;
}
Error message:
.cpp(10): error C2084: function ‘void foo(Age)’ already has a body
You could use templates as:
In this way, each of the typedefs are different types, and cannot be implicitly converted into other types, unless you allow this functionality explicitly in the defintion of the class template
T.