Path: utzoo!attcan!uunet!cs.utexas.edu!tut.cis.ohio-state.edu!pt.cs.cmu.edu!pt!dld From: dld@F.GP.CS.CMU.EDU (David Detlefs) Newsgroups: comp.lang.c++ Subject: Re: Filesystem classes: how to? Message-ID: Date: 8 Jun 90 22:22:34 GMT References: <1567@swbatl.sbc.com> <1642@ns-mx.uiowa.edu> Organization: CMU CS Department Lines: 44 In-reply-to: williams@umaxc.weeg.uiowa.edu's message of 8 Jun 90 15:32:21 GMT Dave Brian Gilstrap sends another scenario, and asks for comments: If you absolutely, positively, can't use virtual functions to do whatever you need to do (and it's a different way of thinking, I've run in to quite a number of situations were I think "shoot, there's no *way* I'm gonna be able to use virtual functions!" but I was eventually able to), well, If you've really gotta be able to get a CharacterFile out of something your program is thinking of as a FileSystemNode, then I'd do something like this: class FileSystemNode { // ... public: virtual CharacterFile* toCharacterFile() { return NULL; } } class CharacterFile : virtual public FileSystemNode { public: CharacterFile* toCharacterFile() { return this; } } Given a FileSystemNode object, I can then invoke toCharacterFile and get back either NULL or a pointer to the object (safely) considered as a CharacterFile. Note that if there is ever a *really* good C++ compiler it may be able to optimize the invocations of these virtual functions away entirely, since they are inline. Drawbacks: By symmetry, you'd want a toX function in FileSystemNode for every subclass X. Therefore, when you add a new subclass Y to FileSystemNode, you need to add a new toY function to the definition of FileSystemNode. This can require a lot of recompilation (virtual table size changes, for one thing), but no reprogramming -- previous code stays correct. For what it's worth... -- Dave Detlefs Any correlation between my employer's opinion Carnegie-Mellon CS and my own is statistical rather than causal, dld@cs.cmu.edu except in those cases where I have helped to form my employer's opinion. (Null disclaimer.)