Limit default arguments

Default arguments are a C++ feature that let you avoid overloading functions in some circumstances. Although they are OK to use, they have several properties that restrict their use.

Avoid more than one or two default arguments. If you have that many options on a function or constructor, rethink your interface. Specifying large numbers of options via function arguments is confusing and not very extensible. Having large numbers of options is also confusing.

Default arguments constitute a form of inline declaration. Specifically, a declaration with default arguments like this

      class TFoo {
          void Bar(int iItem = 10);
is exactly equivalent to the following inline declaration, with all of the consequences that implies (see
"Inline functions" on page 90):

      class TFoo {
          void Bar(int iItem);
          inline void Bar() { Bar(10); };
If you ever want to change the value of the default argument, or if it might ever become convenient to specifically handle the case with no argument, you don't have that option; the call to Bar(int iItem) with a value of 10 is compiled directly into client code. There's no way to go back and make it call Bar() instead.

If you ever want the option of changing what happens, use overloading instead of default arguments.

[Contents] [Previous] [Next]
Click the icon to mail questions or corrections about this material to Taligent personnel.
Copyright©1995 Taligent,Inc. All rights reserved.

Generated with WebMaker