Topic: Const class member
Author: thoff@symantec.com (Torsten Hoff)
Date: 1996/04/02 Raw View
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 ]
Author: kanze@lts.sel.alcatel.de (James Kanze US/ESC 60/3/141 #40763)
Date: 1996/04/03 Raw View
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
]
Author: vandevod@cs.rpi.edu (David Vandevoorde)
Date: 1996/04/03 Raw View
>>>>> "TH" == Torsten Hoff <thoff@symantec.com> writes:
[...]
TH> If you *really* want compile-time initialized constants, and can live
TH> with something that has essentially the same properties as an integer,
TH> use class-scope enums. If you need several compile-time constants with
TH> the same value, you can use multiple untagged enums:
TH> class AnyClass
TH> {
TH> enum {FOO = 1, BAR = 1}; // multiple enums with same value;
// doesn't work!
TH> enum {FOO = 1}; // OK
TH> enum {BAR = 1}; // OK
TH> }
[...]
Alternatively:
struct AnyStruct {
static bool const Yes = true;
static char const A = 'B';
};
(Somewhere:
bool const AnyStruct::Yes;
char const AnyStruct::A;
)
which works for integral types (if you compiler is reasonably up-to-date).
Daveed
---
[ 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
]
Author: brianmcg@interaccess.com (Brian V. McGroarty)
Date: 1996/03/30 Raw View
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?
Would compilers stand a better chance of optimizing given the above via
constant propogation through inlined (member only?) functions applied to a
hard-coded instance of the object, or would it just be to help prevent
programmers' error?
---
Brian Valters McGroarty -- brianmcg@bix.com
phone/fax (847) 439-7714
---
[ 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 ]
Author: tangaroa@werple.net.au (Simon Crase)
Date: 1996/03/31 Raw View
Brian V. McGroarty (brianmcg@interaccess.com) 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
Have you tried:
AnyClass::AnyClass
: constInt(42)
{
}
The geenral idea seems to be to disallow any piece of code that assigns
a value using constInt=....
: 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?
: Would compilers stand a better chance of optimizing given the above via
: constant propogation through inlined (member only?) functions applied to a
: hard-coded instance of the object, or would it just be to help prevent
: programmers' error?
: ---
: Brian Valters McGroarty -- brianmcg@bix.com
: phone/fax (847) 439-7714
: ---
: [ 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 ]
--
Simon A. Crase Emerson & Crase Consultants - ++61-3-9592-1764
Artificial Intelligence, Graphical User Interfaces, C++,
Object Oriented Design, Microsoft Windows Applications
s.crase@ieee.org = tangaroa@werple.net.au
[ 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 ]