mutchler@pitstop.UUCP (Dan Mutchler) (01/31/89)
I am fairly new to C++ so if this is incredibly obvious, forgive me. I am working on a project for my PC where I would like to treat things like windows as a class. I don't have a lot of trouble with that except how to handle an error situation in the constructor for the class. There are a few dependencies that may make creating an object illegal (i.e. invalid font type) and I would like to provide some way for the program creating the object to detect this and handle it appropriately. How can I go about doing this? If my philosphy is completely out of line here, then please point that out as well. Thanks Dan mutchler@sun.UUCP sun!zule!mutchler
vilot@ie0013.dec.com (Michael J. Vilot) (02/02/89)
> how to handle an error situation in the constructor for the class. > ... I would like to provide some way for the program > creating the object to detect this and handle it appropriately. How > can I go about doing this? > mutchler@sun.UUCP > sun!zule!mutchler I use the approach of storing status information in the object (originally outlined by Jon Shopiro in note <7188@alice.UUCP> 17 Aug 87). If there's a possibility that a constructor can fail, then the notion of a "null" object of that class -- that is, a well-formed object that simply has no interesting data values -- is an essential part of the abstraction provided by the class. This gives clients of the class an abstract way to test if the constructor succeeded (similar to testing for a NULL pointer after calling malloc() in old C). Providing member functions for querying the status rounds out the idea -- and delegates the details of how to react to the situation to the client. An alternate approach is to invoke an error reporting function, although I tend to avoid this in constructors. See pp. 207-209, and the discussion of the _new_handler on pp. 92/93 in "The C++ Programming Language" for an outline of this alternative. Hope this helps, Mike Vilot Self-employed. Consulting at: e-mail: vilot%csdpie.dec@decwrl.dec.com when that fails: (508)467-3631 [for now]