Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!wuarchive!zaphod.mps.ohio-state.edu!tut.cis.ohio-state.edu!ucbvax!pasteur!galileo.berkeley.edu!jbuck From: jbuck@galileo.berkeley.edu (Joe Buck) Newsgroups: comp.lang.c++ Subject: Re: Whatever happened to virtual data? Message-ID: <11833@pasteur.Berkeley.EDU> Date: 9 Mar 91 23:21:31 GMT References: <1991Mar7.204853.22289@watmath.waterloo.edu> Sender: news@pasteur.Berkeley.EDU Reply-To: jbuck@galileo.berkeley.edu (Joe Buck) Lines: 40 In article <1991Mar7.204853.22289@watmath.waterloo.edu>, gjditchfield@watmsg.uwaterloo.ca (Glen Ditchfield) writes: [ In reference to the dungeons-and-dragons game problem where the programmer wants a static varible for each derived class ] |> Virtual data might also help programmers who want to implement their own |> type tests. |> My guess is that the proposal was dropped because virtual functions |> could provide the facility without much ugliness. Yes, let's say you want a flag associated with each class derived from class Thing, but you want a separate word for each class. You can get the effect of a virtual static data variable by using a virtual function that returns a reference. That is, class Thing : { ... virtual int& known() = 0; }; class SomeKindOfThing : public Thing { ... int& known() { static int knownFlag; return knownFlag; } }; Notice that the knownFlag static variable inside the function works very much like a virtual static class variable. This is another case that makes it appear that a more powerful template ability is needed, one that can specify how a virtual method is to be overridden in each class: you need to write the above for each derived class, because even though the function is exactly the same, the knownFlag variable is different for each class. -- Joe Buck jbuck@galileo.berkeley.edu {uunet,ucbvax}!galileo.berkeley.edu!jbuck