Path: utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!usc!ucsd!helios.ee.lbl.gov!ux1.lbl.gov!beard From: beard@ux1.lbl.gov (Patrick C Beard) Newsgroups: comp.lang.c++ Subject: Re: Novice question re: class derivation Message-ID: <4654@helios.ee.lbl.gov> Date: 22 Jan 90 01:04:29 GMT References: <44.UUL1.3#5109@pantor.UUCP> Sender: usenet@helios.ee.lbl.gov Reply-To: beard@ux1.lbl.gov (Patrick C Beard) Organization: Lawrence Berkeley Laboratory, Berkeley Lines: 69 X-Local-Date: 21 Jan 90 17:04:29 PST In article <44.UUL1.3#5109@pantor.UUCP> richard@pantor.UUCP (Richard Sargent) writes: #We have encountered a problem designing a tree of classes, that I #hope someone out there can help resolve. # #We want to be able to provide a base class called Display, which has #derived classes for specific types of displays (such as Vista, Targa, #VGA, raster file, etc.). Our first pass results in the following #tree: # # Display # | # +--------+-----+--------+--------+ # | | | | | # Vista Targa VGA RasterFile etc. # This seems like a perfectly reasonable approach to display design. You would want virtual functions defined in the Display class for every operation you would want to support across all of the devices. #Now the problem comes when we try to code for the display device. #Ideally, we want to be able to use an instance of class Display, #so that our code is *written* device independent [sic]. #to simply code "Display device" and apply operations to "device", Here is the case where a switch statement is appropriate: you must decide at run-time what particular instance to allocate. This decision only has to be made once, and is automated throughout the rest of your program by the use of virtual functions. Write a function of the form: Display* AllocDisplay() { Display *device = nil; // default is a display we don't know about. switch (inq_device_type()){ case dv_VISTA: device = new Vista; break; case dv_TARGA: device = new Targa; break; case dv_VGA: device = new VGA; break; /* etc... */ } return device; } [Notice you have to use new to get the object to persist beyond the AllocDisplay function.] This isn't inelegant, it is the only way to do it. Another approach might be to use multiple inheritance and have the object you allocate inherit methods for all devices, and knows internally what device it is talking to, but I think that it would probably be a rat's nest to implement. The one drawback I see to the above method, is that if you add more display types, you'll have to change this function to handle more types. But the changes will be isolated to this one function, all the rest of your code only has to know about the base class. I think this brings up an important issue: use object oriented technology where appropriate, standard functional programming where appropriate. ------------------------------------------------------------------------------- - Patrick Beard, Macintosh Programmer (beard@lbl.gov) - - Berkeley Systems, Inc. "..............Good day!" - Paul Harvey - -------------------------------------------------------------------------------