home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- From: nikki@trmphrst.demon.co.uk (Nikki Locke)
- Path: sparky!uunet!pipex!demon!trmphrst.demon.co.uk!nikki
- Subject: Re: Any inheritance experts out there
- Reply-To: nikki@trmphrst.demon.co.uk
- Distribution: world
- X-Mailer: cppnews $Revision: 1.30 $
- Organization: Trumphurst Ltd.
- Lines: 46
- Date: Mon, 21 Dec 1992 17:49:33 +0000
- Message-ID: <724985373snx@trmphrst.demon.co.uk>
- Sender: usenet@demon.co.uk
-
- In article <4019@hpwala.wal.hp.com> friedman@hp-and.an.hp.com (joel friedman) writes:
- > I am writing device drivers for external memory devices. I currently
- > have to support two types devices (say A and B), but that number could
- > grow. The functions each device must support are common, but the
- > implementations used to provide them are quite different for each. The
- > type T below can be a char xor a short. The following template class
- > defines these core functions:
- [ Template omitted. Oh, and do you think you could word-wrap your postings ?
- It makes them a lot easier to deal with. ]
-
- > I want to design the hierarchy so I can easily add device C, D, E etc.
- > Here's the catch. How can I do this so that the application programs only
- > need to see the core_function class declaration; not the derived class
- > declarations for A or B. I need to enhance the core_functions class so
- > that the user can call a core_functions function, and it will be directed
- > to the implementation in the proper derived class (A or B).
-
- To start with, have a base class called something sensible and
- understandable, like maybe "Device". Make the core functionality virtual.
- Derive A, B (and maybe others) from it.
-
- Now, who or what determines the type of device with which the application
- is dealing ? I assume (because you don't want the application to know
- about device types) that you have some function in your library which
- determines the actual type of device which is connected, and the
- application calls this. So, make the function retern a reference to a
- Device. The function itself constructs an A, B (or C ...) and returns a
- reference to it. The application continues to treat the returned Device
- reference as a Device reference. It does not need to know what kind of
- device it is - it just calls virtual functions for the Device, and
- everything works.
-
- Example :
-
- Device &determineWhatKindOfMouseWeHaveAndReturnAReferenceToIt() // :-)
- {
- if(wierdAndWonderfulIoctlCall(ISTHISASERIALMOUSE))
- return *new SerialMouse;
- if(anotherStrangeHardwareCall(ISTHISABUSMOUSE))
- return *new BusMouse;
- return *new NotVeryUsefulKeyboardDeviceThatEmulatesAMouse;
- }
-
- --
- Nikki Locke,Trumphurst Ltd.(PC and Unix consultancy) nikki@trmphrst.demon.co.uk
- trmphrst.demon.co.uk is NOT affiliated with ANY other sites at demon.co.uk.
-