Differentiate overloaded constructors

Sometimes you want to overload constructors, but discover that the argument types you want to use are not sufficient to differentiate those constructors. A good solution is to use a lightweight (inline) nested class to wrap constructor arguments in a distinct type that's easy to overload. For example, in the Graphics system, one early design for TGrafMatrix had constructors that were similar to:

      TGrafMatrix(const Translate &);
      TGrafMatrix(const Rotate &);
      TGrafMatrix(const Scale &);
The three argument types are nested classes whose constructors take appropriate arguments. A call to create a translation matrix looks like this:

    TGrafMatrix(TGrafMatrix::Translate(point));
Eventually, with name spaces, the helper classes might not have to be nested, which will make this kind of construct easier to type.


[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