Newsgroups: comp.lang.c++ Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!think.com!snorkelwacker.mit.edu!stanford.edu!neon.Stanford.EDU!news From: philip@pescadero.stanford.edu (Philip Machanick) Subject: Re: C++ Static Members and Inheritance Message-ID: <1991May5.224348.12578@neon.Stanford.EDU> Sender: news@neon.Stanford.EDU (USENET News System) Organization: Computer Science Department, Stanford University References: <5069@servax0.essex.ac.uk> <7510@harrier.ukc.ac.uk> Date: Sun, 5 May 1991 22:43:48 GMT Lines: 38 In article <7510@harrier.ukc.ac.uk> mr3@ukc.ac.uk (M.Rizzo) writes: >In article <5069@servax0.essex.ac.uk> whisd@essex.ac.uk (Whiteside S D B) writes: > >>I had a base class in which I declared a static data member. >> >>I then derived TWO separate classes which both inherited from the base class. >> >>Then these classes were instanstiated as objects I found that for BOTH the static data member (a pointer) had the SAME value. [...] >The only solution I found was to re-declare the static data members >in every subclass I needed them. However this should be done with >care. If a base class member function refers to a static data member, >then if this function is inherited by a derived class re-declaring the >same static member, the base class static member will still be the one >referenced by the inherited function. > >What we need here is virtual static data members ! Unfortunately there >are no such thingies :-( > >To solve this problem you can define two virtual non-static functions >to read and write the member - though again this must be done for >every derived class where the static member is declared. Macros are very >handy here ! > >Does anybody know of another (possibly better) approach ? [...] Make the statics private (rather than protected). Then you are forced to redeclare them for derived classes. This still doesn't help you if you call a function higher up the hierarchy - the only solution I can propose here is that you pass the statics as parameters at all times. This, combined with the use of private, ensures you get the right one. Again - a better approach would be appreciated. Philip Machanick