🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: Const class member

#3887
From: Torsten Hoff
Date: Tue, 02 Apr 1996 13:30
76 lines
3471 bytes
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!
    enum {FOO = 1};          // OK
    enum {BAR = 1};          // OK
}

To continue with the const class member for a moment, you can't
initialize the const class member in your constructor, as you already
noted -- this must be done using the initialization list. You can do
this as follows:

AnyClass::AnyClass() : constInt(0)
    {
    // other initialization code goes here
    }

or

AnyClass::AnyClass(int ConstValue) : constInt(ConstValue)
    {
    // other initialization code goes here
    }

The initialization is actually performed before your constructor is
entered; if more than one member is being initialized, it happens in
the order in which the members are declared in the class, *not* the
order in which they are listed in the initialization list.

I hope this helps!


Torsten Hoff
thoff@symantec.com

(The views and opinions expressed are my own, and
should not be construed as representing those of
Symantec Corporation)
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
Technical Details
Message-ID: <4jq60l$6pp@symiserver2.symantec.com>
Path: arc1-ca.pugleaf.net!in2.uu.net!bounce-back
References: <4jgpqa$t09@nntp.interaccess.com>

Show Full Headers
Complete Article Headers
From: thoff@symantec.com (Torsten Hoff)
Message-ID: <4jq60l$6pp@symiserver2.symantec.com>
X-Original-Date: Tue, 02 Apr 96 00:07:25 GMT
Path: arc1-ca.pugleaf.net!in2.uu.net!bounce-back
Date: 02 Apr 96 13:30:31 GMT
Approved: fjh@cs.mu.oz.au
Newsgroups: comp.std.c++
Subject: Re: Const class member
Organization: Symantec Corp.
References: <4jgpqa$t09@nntp.interaccess.com>
X-Newsreader: News Xpress Version 1.0 Beta #4
X-Auth: PGPMoose V1.1 PGP comp.std.c++
 iQBFAgUBMWEsI+EDnX0m9pzZAQHDQAF/ZG6X3PJ16+fPWTc2h6Ei69iIAgSyNTZ0
 2l92eD6qpxVO/OhI4nKyRB0nNA4+G5Sb
 =YCiC