Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!zaphod.mps.ohio-state.edu!wuarchive!uunet!paradim!jean From: jean@paradim.UUCP (Jean Pierre LeJacq) Newsgroups: comp.lang.c++ Subject: Re: Design Problem Summary: Using polymorphism Message-ID: <192@paradim.UUCP> Date: 11 Dec 89 01:09:37 GMT References: <22137@brunix.UUCP> Organization: Rush Systems Inc., Milford, NH Lines: 24 In article <22137@brunix.UUCP>, sdm@cs.brown.edu (Scott Meyers) writes: > I find myself with a collection of objects of a given type, where some of > them define a particular function and some of them don't, and I want to > invoke the function on only those objects for which it is defined. .... > > It seems I have three possible approaches: > 1. Define evaluate() as a virtual function in ARC, and redefine it > appropriately in EXECUTABLE_ARC. In ARC, make it a noop, so when > called on NONEXECUTABLE_ARCs, it won't do anything. > 2. Add an enumerated type to ARC that enumerates each of the subtypes, > and add a virtual function arc_type() that is redefined in each > subclass to return the appropriate value. ... > 3. Separate my ARC class into two distinct classes, so I never have > this problem. Another approach is to insure that only instances of EXECUTABLE_ARC are inserted into a collection. You are then assured that all members of the collection, including public subclasses, have evaluate() defined. This will require redesigning the solution to your problem at a higher level. For example, two collections may be required to manage the two fundamental ARC classes. The goal is to use polymorphism to your advantage instead of reverting to C "switch" approaches or to de-tune your class definitions.