Use static members as constructors

When the standard constructor mechanism is too inflexible, you can use a static member function that calls a private constructor to create a partially valid object, then finishes building it, and returns the result.

Static member constructors are useful because they can have different names, and you can force an object to be on the heap by making all constructors private. The disadvantage is that if you want the object to work both heap allocated and non-heap allocated, you need two versions of the static function, and the nonheap version must copy the object.

This technique can be useful occasionally, but use it sparingly.


[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