Article View: comp.std.c++
Article #211Re: Scope of static member initializers
From: Mike Lijewski
Date: Wed, 01 Aug 1990 12:37
Date: Wed, 01 Aug 1990 12:37
62 lines
2428 bytes
2428 bytes
In article <1990Aug1.030632.2273@agate.berkeley.edu> lippin@math.berkeley.edu writes:
>My pet peeve with C++ is that (accordong to CFront and a too-close
>reading of the AT&T language description) initializers for static data
>members of a class are in the public scope, rather than the scope of
>the class.
I don't believe the initializers for static data members of a class are
in the public scope. Consider the following:
#include <stream.h>
class X {
public:
static const int i;
static const int j;
};
const int X::i = 1;
const int X::j = i;
int main() { cout << "X::i = " << X::i << ", X::j = " << X::j; }
This compiles cleanly with cfront 2.0. This proves that the initialization
of X::j is done in the scope of the class. My understanding is that whenever
you define something which is prefixed with classname::, you are in the
scope of `classname'. This is how you can define member functions outside
of the class declaration and still be within the scope of the class.
>class groupmember {
> public:
> static const groupmember zero;
> ...
>Zero can only be initialized through a public constructor,
Zero can be initialized with a private constructor. Embellishing your
class a bit we have:
#include <stream.h>
class groupmember {
public:
static const groupmember zero;
void f() const { cout << "p = " << p << "\n"; }
private:
int p;
groupmember() { p = 1; }
};
groupmember const groupmember::zero = groupmember();
int main() { groupmember::zero.f(); return 0; } // will print 'p = 1'
Again, this compiles cleanly under cfront 2.0, so the initialization of
groupmember::zero is again in the scope of the class.
--
Mike Lijewski (H)607/277-0394 (W)607/254-8686
Cornell National Supercomputer Facility
ARPA: mjlx@eagle.cnsf.cornell.edu BITNET: mjlx@cornellf.bitnet
SMAIL: 1122 Ellis Hollow Rd. Ithaca, NY 14850
Technical Details
Message-ID:
<10603@batcomputer.tn.cornell.edu>Path:
arc1-ca.pugleaf.net!utzoo!attcan!uunet!zephyr.ens.tek.com!uw-beaver!cornell!batcomputer!lijewskiReferences:
<1990Aug1.030632.2273@agate.berkeley.edu>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!attcan!uunet!zephyr.ens.tek.com!uw-beaver!cornell!batcomputer!lijewski From: lijewski@batcomputer.tn.cornell.edu (Mike Lijewski) Newsgroups: comp.std.c++ Subject: Re: Scope of static member initializers Message-ID: <10603@batcomputer.tn.cornell.edu> Date: 1 Aug 90 12:37:45 GMT References: <1990Aug1.030632.2273@agate.berkeley.edu> Reply-To: lijewski@tcgould.tn.cornell.edu (Mike Lijewski) Organization: Cornell National Supercomputer Facility Lines: 62