Create objects in a valid state

Construct objects in a valid, ready-to-use state, with all public member functions ready to do something sensible. Don't create objects in an invalid state and then expect the client to call an open routine, or to call a close routine before destruction.

Always allocate needed resources in the constructor because it is more convenient, and they are properly freed by the destructor if an exception occurs and the object is destroyed in a stack unwind.

NOTE There are only two exceptions to this rule. One is the virtual Initialize(). (For more information about Initialize(), see "Base class constructors cannot call virtual functions" on page 96.) The other exception is that a constructor may make an invalid object to be streamed into, but it must still be valid enough for the destructor to run.

[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