O'Reilly logo

Sams Teach Yourself C++ in 24 Hours, Third Edition by Jesse Liberty

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Choosing Between Default Values and Overloaded Functions

Listings 13.1 and 13.2 accomplish the same thing, but the overloaded functions in 13.1 are simpler to understand and more natural to use. Also, if a third variation is needed—perhaps the user wants to supply either the width or the height, but not both—it is easy to extend the overloaded functions. The default value, however, will quickly become too complex to use as new variations are added.

How do you decide whether to use function overloading or default values? Here's a rule of thumb:

Look to function overloading when

  • There is no reasonable default value.

  • You need different algorithms.

  • You need to support differing types in your parameter list.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required