Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!swrinde!cs.utexas.edu!asuvax!ncar!dinl!noren From: noren@dinl.uucp (Charles Noren) Newsgroups: comp.lang.c++ Subject: Re: C++ Static Members and Inheritance Message-ID: <1858@dinl.mmc.UUCP> Date: 9 May 91 21:40:51 GMT References: <5069@servax0.essex.ac.uk> <7510@harrier.ukc.ac.uk> <1991May5.224348.12578@neon.Stanford.EDU> <520@heurikon.heurikon.com> Reply-To: noren@dinl.UUCP (Charles Noren) Organization: Martin Marietta I&CS, Denver CO. Lines: 42 In article <520@heurikon.heurikon.com> daves@ex.heurikon.com (Dave Scidmore) writes: > >Perhaps I am dense, but I would assume that if a static member variable >was declared in a base class it is because you want only one instance of >that variable to exist for that class and all those derived from it. I >am having trouble envisioning a situation where you would want multiple >static member variable (a contradiction in terms IMHO). There are cases where you may want multiple instances of static members of the same name. One example of use is in the NIH Class Library (NIHCL). A good description of its use is found in the book DATA ABSTRACTION AND OBJECT- ORIENTED PROGRAMMING IN C++, by Gorlen, Orlow, Plexico (1990, John Wiley & Sons, ISBN 0-471-92346-X), pp 144-146. In NIHCL, the static member classDesc is defined for each class, and you must define it for each class you derive from another NIHCL class (this is done for you in their DECLARE_MEMBERS and DEFINE_CLASS macros). The classDesc member is an instance of NIHCL's Class class which keeps certain information specific to the class it is defined in. Its address is also used to identify what class an object belongs to. For instance, every class implements the member function isA usually as: const Class* X::isA() const {return &classDesc;} Again, this is provided for the NIHCL applications user inside the macro DEFINE_CLASS. Certainly there are other ways of providing class identification, but this gives a flavor of what can be done by "overloading" a static member with another of the same name. BTW, using this mechanism inside NIHCL's castdown functions also provides a safe mechanism for doing "down casting" of object pointer from classes with virtual base objects (described on pp. 325-328 of DATA ABSTRACTION). -- Chuck Noren NET: dinl!noren@ncar.ucar.edu US-MAIL: Martin Marietta I&CS, MS XL8058, P.O. Box 1260, Denver, CO 80201-1260 Phone: (303) 977-1646