Article View: comp.std.c++
Article #3909Re: Const class member
From: James Kanze US/ESC 60/3/141 #407...
Date: Wed, 03 Apr 1996 17:22
Date: Wed, 03 Apr 1996 17:22
74 lines
3866 bytes
3866 bytes
In article <4jq60l$6pp@symiserver2.symantec.com> thoff@symantec.com
(Torsten Hoff) writes:
|> In article <4jgpqa$t09@nntp.interaccess.com>,
|> brianmcg@interaccess.com (Brian V. McGroarty) wrote:
|> >Is this legal? Borland and Microsoft compilers will accept the following:
|> >
|> >class AnyClass
|> >{
|> > const int constInt;
|> >}
|> >
|> >The Borland compiler complains about the uninitialized constant, whereas
|> >the Microsoft compiler does not. If a constructor is present, both will
|> >complain that the constant isn't initialized in the constructor, however
|> >neither will allow you to assign a value in the constructor by simply
|> >specifying "constInt= some value". I have also attempted to initialize in
|> >a global variable "int AnyClass::constInt", to determine whether static
|> >somehow became implicit -- still no go. If this is legal, how is the value
|> >initialized?
|> [Snip]
|> That's perfectly legal.
|> However, you can't initialize the const member in the class
|> declaration, since you haven't instantiated an object of the class in
|> question which could receive the value. Furthermore, it would prevent
|> you from assigning different values to constInt in different instances
|> of the class, which in most cases is not what you want, either.
|> If you *really* want compile-time initialized constants, and can live
|> with something that has essentially the same properties as an integer,
|> use class-scope enums. If you need several compile-time constants with
|> the same value, you can use multiple untagged enums:
|> class AnyClass
|> {
|> enum {FOO = 1, BAR = 1}; // multiple enums with same value; doesn't work!
Why not? It is allowed by the standard, and works on all of the
compilers I've used.
|> enum {FOO = 1}; // OK
|> enum {BAR = 1}; // OK
|> }
The (minor) problem with this solution is that the type of the constant
may not be what you want. This is probably only a problem when function
overloading is present, or templates.
The draft standard allows an initialization of a *static* const in the
class definition, e.g.:
class X
{
static size_t const mySize = 1024 ;
} ;
This is a relatively new feature, however, and may not be supported by
your compiler.
[Rest of correct answer deleted...]
--
James Kanze Tel.: (+33) 88 14 49 00 email: kanze@gabi-soft.fr
GABI Software, Sarl., 8 rue des Francs-Bourgeois, F-67000 Strasbourg, France
Conseils, études et réalisations en logiciel orienté objet --
-- A la recherche d'une activité dans une region francophone
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Technical Details
Message-ID:
<KANZE.96Apr3112803@slsvgqt.lts.sel.alcatel.de>Path:
arc1-ca.pugleaf.net!fido.asd.sgi.com!austernReferences:
<4jgpqa$t09@nntp.interaccess.com> <4jq60l$6pp@symiserver2.symantec.com>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!fido.asd.sgi.com!austern From: kanze@lts.sel.alcatel.de (James Kanze US/ESC 60/3/141 #40763) Newsgroups: comp.std.c++ Subject: Re: Const class member Date: 03 Apr 1996 09:22:42 PST Organization: GABI Software, Sarl. Approved: austern@isolde.mti.sgi.com Message-ID: <KANZE.96Apr3112803@slsvgqt.lts.sel.alcatel.de> References: <4jgpqa$t09@nntp.interaccess.com> <4jq60l$6pp@symiserver2.symantec.com> NNTP-Posting-Host: isolde.mti.sgi.com X-Original-Date: 03 Apr 1996 09:28:02 GMT In-Reply-To: thoff@symantec.com's message of 02 Apr 96 13:30:31 GMT Apparently-To: std-c++@ncar.ucar.edu X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBVAwUBMWKz40y4NqrwXLNJAQHmqQH/QO9zQR156yDlXv183f8UEkbm7dIwNxja unq6rIc8xa8ZI49b2kpg724yrO8MQWbOYW8r7vJjWMf6jjX+gtILzA== =FWhZ Originator: austern@isolde.mti.sgi.com