Function Overloading is useful when you have a function that you want
to do several different things with different results depending on what
inputs it is given.
Example :
// Example 1
int Double( int value )
{
return 2 * value;
}
That would return an int, but what if you wanted to double a float?
MSVC++6 would give you a warning saying that converting this could
result in data loss.
Example :
float value = Double( 2.3 );
This would give you 2 warnings. 1 in Double and 1 because of the = operator.
Remember, Double was declared as an int, and float = int results in a warning.
What could you do? You could cast it, but the 2.3 would still be lost, turned into 2.
How would you solve this?
You could do this :
// Example 2
float DoubleForFloats( float value )
{
return value * 2.0;
}
but then you'd have to write and call a different function. Here's how it's done with Function Overloading :
// Example 3
int Double( int value ) { return value * 2; };
float Double( float value ) { return value * 2.0; };
As you can see both functions have the same name, but their type is
different and the value you pass is off a different type as well. Yet
your compiler doesn't give you an error of duplicate declaration. (at
least mine didn't :D)
Function Overloading can only work if the first parameter is different.
You can have ...
int double( int, int );
... and ...
int double( float, int );
... but NOT ...
int double( int, float );
... because the last one would have the first parameter the same as the first one, an int.
With this you can use the same function name, but give out different results.
Simple Example:
// Example 4 :]
#include "stdio.h"
int PrintValue( int value, int repeat );
int PrintValue( float value, int repeat );
int main()
{
int the_int = 10;
float the_float = 12;
PrintValue( the_int, 3 );
PrintValue( the_float, 3 );
return 0;
}
int PrintValue( int value, int repeat )
{
for ( int a = 1; a < repeat + 1; a++ )
{
printf("The int value is %d, and this has been
repeated %d times...\n", value, a );
}
return 0;
}
int PrintValue( float value, int repeat )
{
for ( int a = 1; a < repeat + 1; a++ )
{
printf("The float value is %f, and this has been
repeated %d times...\n", value, a );
}
return 0;
}
You could just as well use this for Half-Life, to change health for
monsters. You could cast this, but you wouldn't be able to call monster
specific stuff.
Example:
// Example 5
int SetHealth( CBaseEntity *pEntity, int health );
int SetHealth( CBasePlayer *pPlayer, int health );
int SetHealth( CBaseMonster *pMonster, int health );
.. Call this with SetHealth( pEntity, 100 ); ..
Of course this seems a bit stupid, but I couldn't think up a better example for Half-Life :]
Their bodies would be very alike ...
Example for the CBaseEntity:
// Example for the CBaseEntity
int SetHealth( CBaseEntity *pEntity, int health );
{
if ( pEntity && pEntity->IsAlive() )
pEntity->pev->health = health;
return 0;
}
But even this way you'd already save a line, as all you have to do
then is SetHealth( pEntity, 100 ); and it would check for you if the
entity you send is both existing AND alive, saving you the if line and
the risk of crashes if you don't do that.
Function Overloading is there to make your code smaller and easier to
read. Overuse, and especially bad overuse can make your code harder to
read, so be careful how you use it. |