johnb@srchtec.UUCP (John Baldwin) (07/26/90)
Short question here:
I am working on a large project involving C++. There are no "C++ gurus"
here (a few of us, myself included, are very familiar with C, however).
There are some code fragments we're trying to understand, one of which
includes a class of storage-type "static". The declaration looks like this:
static class Widget {
.
. // data and member functions here
.
};
We have checked Lippman's book, the Stroustrup text, and Hansen's "answer
book" to no avail. Please forgive me asking this on the net, but what does
it do??
My guess is that you get a class which you can inherit from, but there's only
one instance of it, and all the derived classes can access this instance.
Please respond by email only, to save net bandwidth. I'll post the answer
if anyone's curious. Sorry if this is obvious folklore-type knowledge; we
need to come up to speed very quickly.
Thanks!
--
John T. Baldwin | johnb@srchtec.uucp
Search Technology, Inc. | johnb%srchtec.uucp@mathcs.emory.edu
standard disclaimer: | ...uunet!samsung!emory!stiatl!srchtec..
opinions and mistakes purely my own. | ...mailrus!gatech!stiatl!srchtec...
sdm@cs.brown.edu (Scott Meyers) (07/26/90)
In article <155@srchtec.UUCP> johnb@srchtec.UUCP (John Baldwin) writes: > > static class Widget { > . > . // data and member functions here > . > }; > >We have checked Lippman's book, the Stroustrup text, and Hansen's "answer >book" to no avail. Please forgive me asking this on the net, but what does >it do?? From the E&S ARM (that should make everybody happy), p. 98: The static and extern specifiers can be applied only to names of objects and functions. In particular, static class X { // error int a; // ... }; is illegal. Interestingly, both cfront and g++ accept this kind of thing without complaining. Which brings us back to the original question, actually: given that it's illegal but accepted, what does it do? Scott
johnb@srchtec.UUCP (John Baldwin) (07/27/90)
I apologize, but my previous posting removed *too many* of the details.
Perhaps this is more helpful:
static class Widget {
static int several_data_items; // all static
static void several_memb_funcs(); // once again, all static
} widg_inst;
Is this any better? (P.S. it runs on a lot of different platforms)
--
John T. Baldwin | johnb@srchtec.uucp
Search Technology, Inc. | johnb%srchtec.uucp@mathcs.emory.edu
standard disclaimer: | ...uunet!samsung!emory!stiatl!srchtec..
opinions and mistakes purely my own. | ...mailrus!gatech!stiatl!srchtec...
wallis@labc.enet.dec.com (Barry L. Wallis) (07/27/90)
In article <45882@brunix.UUCP>, sdm@cs.brown.edu (Scott Meyers) writes...
// Original question by John Baldwin omitted
;
;From the E&S ARM (that should make everybody happy), p. 98:
;
; The static and extern specifiers can be applied only to names of
; objects and functions. In particular,
;
; static class X { // error
; int a;
; // ...
; };
;
; is illegal.
;
;Interestingly, both cfront and g++ accept this kind of thing without
;complaining. Which brings us back to the original question, actually:
;given that it's illegal but accepted, what does it do?
;
It can do anything it darn well pleases ;-), so use it at your own risk.
---
Barry L. Wallis USENET: wallis@labc.dec.com
Database Consultant "Everything is an object except this which
U.S. DECtp Resource Center is a pointer."
Los Angeles, CA No one voted for me, I represent myself
---
steve@taumet.com (Stephen Clamage) (07/27/90)
johnb@srchtec.UUCP (John Baldwin) writes: >static class Widget { > static int several_data_items; // all static > static void several_memb_funcs(); // once again, all static > } widg_inst; The static refers to widg_inst, exactly as in ordinary C. There is no such thing as a static class, only static objects. You could also write this as class Widget { ... }; static Widget widg_inst; and it would mean exactly the same thing -- just like ordinary C. -- Steve Clamage, TauMetric Corp, steve@taumet.com
johnb@srchtec.uucp (John Baldwin) (08/01/90)
Many thanks to all those who sent helpful email messages. You can stop sending mail now. Really. [Not only did I get the answer (which should have been obvious to me), but I also noticed a curious effect: most email responses were posted after both my initial posting and the followup correction, yet (most) only referred to the first posting. Wunderwhyizzithat? :-) ]