< Previous | Contents | Next >

Calling Overloaded Functions

You can call an overloaded function the same way you call any other function, by using its name with a set of valid arguments. But with overloaded functions, the compiler (based on the argument values) determines which definition to invoke. For example, when I call triple() with the following line and use an int as the argument, the compiler knows to invoke the definition that takes an int. As a result, the function returns the int 15.

cout << "Tripling 5: " << triple(5) << "\n\n";

I call triple() again with:

cout << "Tripling ’gamer’: " << triple("gamer");

Because I use a string literal as the argument, the compiler knows to invoke the definition of the function that takes a string object. As a result, the function returns the string object equal to gamergamergamer.