🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #2586

Re: Virtual static members

#2586
From: Matt Austern
Date: Tue, 14 Nov 1995 01:12
47 lines
3042 bytes
In article <488hg6$prm@engnews1.eng.sun.com>
clamage@Eng.Sun.COM (Steve Clamage) writes:

> In your example, we might have
> 	A a;
> 	A* ap = new B;
> 	a.s   // gets A::s
> 	ap->s // gets B::s
> 
> That's workable, if a bit kludgy, but it isn't obvious to me (among others)
> what programming problem is solved by this extra complication that can't be
> solved adequately already. By that, I don't mean "Can you think of a problem
> that this extension solves?" I do mean "Considering the criteria expressed
> in D&E and other places for extensions to C++, is this a good candidate?"

My own opinion is that virtual static members would occasionally be
useful, and I do occasionally miss them, but that it is already
possible to do pretty much everything that you'd want to use virtual
static members for.  

The case when they would be nice is if you have some piece of
information that is shared between all objects of a class, but that is
different for each class in a hierarchy.  Some sort of type code, for
example, that you use when you're saving object to and restoring them
from permanent storage.  It's useful, in cases like that, to be able
to refer to something like that both as B::code() and as A->code().

There are other ways you can achieve the same effect, though.  The
most obvious one is just to have two functions for each class in your
hierarchy, one static and one virtual, where the virtual function
simply calls the static function.  Another solution is to define only
the virtual function, and then provide an exemplar for each class in
your hierarchy; instead of B::code(), you'd write something like
B_exemplar.code().

To answer Steve's question, then, I don't think that virtual static
functions are a good candidate for a C++ extension.  They solve a real
problem, but it's a problem that can be solved already.

-- 
  Matt Austern                             He showed his lower teeth.  "We
  matt@physics.berkeley.edu                all have flaws," he said, "and
  http://dogbert.lbl.gov/~matt             mine is being wicked."
---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Technical Details
Message-ID: <MATT.95Nov13154705@cyclops5.berkeley.edu>
Path: arc1-ca.pugleaf.net!in2.uu.net!bounce-back
References: <487q1e$peq@aztec.co.za> <488hg6$prm@engnews1.eng.sun.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!in2.uu.net!bounce-back
Date: 14 Nov 95 01:12:51 GMT
Approved: fjh@cs.mu.oz.au
From: matt@cyclops5.berkeley.edu (Matt Austern)
Newsgroups: comp.std.c++
Subject: Re: Virtual static members
X-Original-Date: 13 Nov 1995 23:47:05 GMT
Organization: University of California at Berkeley (computational neuroscience)
Message-ID: <MATT.95Nov13154705@cyclops5.berkeley.edu>
References: <487q1e$peq@aztec.co.za> <488hg6$prm@engnews1.eng.sun.com>
Reply-To: matt@physics.berkeley.edu
In-Reply-To: clamage@Eng.Sun.COM's message of 13 Nov 1995 22:58:09 GMT
X-Auth: PGPMoose V1.1 PGP comp.std.c++
 iQBFAgUBMKftVuEDnX0m9pzZAQGUlwF/cDlTEAHgnVIEEs7L/SO9xsCgtY9LucrT
 qZu0HABb6MpmG01mw6PP7fpXsl5kuSSc
 =x55+