Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!thunder.mcrcim.mcgill.edu!snorkelwacker.mit.edu!think.com!spool.mu.edu!uunet!mcsun!ukc!slxsys!ibmpcug!demon!news From: gtodd@cix.compulink.co.uk (Giles Todd) Newsgroups: comp.lang.c++ Subject: Re: const in a class Message-ID: <1991Jun17.184324.1979@demon.co.uk> Date: 17 Jun 91 18:43:24 GMT Sender: news@demon.co.uk (C-News Owner) Reply-To: Giles Todd Organization: Gated to News by demon.co.uk Lines: 42 In-Reply-To: <768@taumet.com> steve@taumet.com (Stephen Clamage) steve@taumet.com (Stephen Clamage) writes: > Chris_Sullivan@kcbbs.gen.nz (Chris Sullivan) writes: > > >I want to be able to declare a local constant in a class ie a constant > >whose scope is restricted to the class... > > >class C { > > const int size = 1000; > > char array[size]; > > ... > > } > > Unfortunately, this is not supported in C++. The best you can do > is to declare an enum: > class C { > enum { size = 1000 }; > char array[size]; > ... > }; Careful here. The value of an enumerator must be an int or a value that can be promoted to int by integral promotion. If the compiler implementor has decided to represent enums with chars then your example will overflow (perhaps silently). For safety's sake, limit the values of enums to the range 0..127. Giles. -- Giles Todd gtodd%cix@specialix.co.uk (Internet) MCR1:RUNDART (GeoNet) Voice: +44 925 33472 Data: +44 925 33472 (voice call first please) Rundart Ltd, 5 Brentnall Close, Great Sankey, Warrington, WA5 1XN, UK