Condividi tramite


Overview of Overloading

With the C++ language, you can overload functions and operators. Overloading is the practice of supplying more than one definition for a given function name in the same scope. The compiler is left to pick the appropriate version of the function or operator based on the arguments with which it is called. For example, the function max is considered an overloaded function. It can be used in code such as the following:

// overview_overload.cpp
double max( double d1, double d2 )
{
   return ( d1 > d2 ) ? d1 : d2;
}

int max( int i1, int i2 )
{
   return ( i1 > i2 ) ? i1 : i2;
}
int main()
{
   int    i = max( 12, 8 );
   double d = max( 32.9, 17.4 );
}

In the first function call, where the maximum value of two variables of type int is being requested, the function max( int, int ) is called. However, in the second function call, the arguments are of type double, so the function max( double, double ) is called.

See Also

Concepts

Overloading