Article View: comp.std.c++
Article #2761Re: Can I declare private constants?
From: Steve Clamage
Date: Tue, 28 Nov 1995 19:25
Date: Tue, 28 Nov 1995 19:25
65 lines
2522 bytes
2522 bytes
In article 62g@sunsystem5.informatik.tu-muenchen.de, schuenem@informatik.tu-muenchen.de (Ulf Schuenemann) writes:
>|> >class X
>|> >{
>|> >static const int fifty;
>|> >};
>|> >int X::fifty = 50;
> ^^^^^^^^^^^^^^^^^ (*)
>|> But why not use the method that preserves the original intent of having
>|> fifty be a manifest constant?
>|>
>|> class X {
>|> static const int fifty = 50;
>|> };
>|> and then in one place
>|> int X::fifty;
>Coz I've to designate once and forever one file to contain this
>definition and to link it whenever I use class X.
>const int X__fifty = 50;
>It this works, why cant (*) work the same way ? The only
>difference is, that X::fifty is a member and X__fifty isn't.
That isn't the only difference. X::fifty is (a single) global variable.
X__fifty is not global. It has static linkage, and there might be one in
every compilation unit.
The appropriate comparison would be with
extern const int X__fifty = 50;
which would appear in exactly one place, and
extern const int X__fifty;
every place else.
That said, the One-Definition Rule for inline member functions requires
that the compiler have some mechanism for ensuring exactly once copy of
things which have multiple definition points.
Example:
header.h:
========
class X {
virtual int f() { .... }
// there can be only one X::f() in the whole program
};
Templates present a similar problem, since the template instantiation
code for an external function might appear in several compilation units.
Since the compiler must have such a mechanism anyway, it seems to me
that we could eliminate the requirement for a single point of
definition of static members. I don't know of any such proposal
before the C++ committee, however.
---
Steve Clamage, stephen.clamage@eng.sun.com
---
[ 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:
<49fne0$nbl@engnews1.eng.sun.com>Path:
arc1-ca.pugleaf.net!128.32.155.1!mattReferences:
<49fe01$62g@sunsystem5.informatik.tu-muenchen.de>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!128.32.155.1!matt From: clamage@Eng.Sun.COM (Steve Clamage) Newsgroups: comp.std.c++ Subject: Re: Can I declare private constants? Date: 28 Nov 1995 19:25:04 GMT Organization: Sun Microsystems Inc. Approved: matt@physics2.Berkeley.EDU (Matt Austern) Message-ID: <49fne0$nbl@engnews1.eng.sun.com> References: <49fe01$62g@sunsystem5.informatik.tu-muenchen.de> Reply-To: clamage@Eng.Sun.COM NNTP-Posting-Host: physics2.berkeley.edu