glenn@bitstream.com (Glenn P. Parker) (06/14/91)
In article <1991Jun15.014436.10060@kcbbs.gen.nz> Chris_Sullivan@kcbbs.gen.nz (Chris Sullivan) writes: > class C { > const int size = 1000; > char array[size]; > ... > } This is a hack, but it has been suggested by greater authorities than me! class C { enum { size = 1000 }; char array[size]; // ... }; -- Glenn P. Parker glenn@bitstream.com Bitstream, Inc. uunet!huxley!glenn 215 First Street BIX: parker Cambridge, MA 02142-1270
steve@taumet.com (Stephen Clamage) (06/14/91)
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]; ... }; -- Steve Clamage, TauMetric Corp, steve@taumet.com
Chris_Sullivan@kcbbs.gen.nz (Chris Sullivan) (06/15/91)
I want to be able to declare a local constant in a class ie a constant
wgose scope is restricted to the class. This prevents any problems
with a constant of the same name having scope outside the class.
I tried something like the following but Zortech and Turbo C++ don't
appear to accept const in a class :-
class C {
const int size = 1000;
char array[size];
...
}
In the end I had to replace the const with #define size 1000
but this changes any mainfest constant with the same name appearing
outside the scope of class C. I understand that const's are preffered
to #define in c++ but I can't find a way to do what I want. Any ideas
anyone?
amb@Apple.COM (A. Michael Burbidge) (06/15/91)
In article <GLENN.91Jun14100535@huxley.huxley.bitstream.com>, glenn@bitstream.com (Glenn P. Parker) writes: > In article <1991Jun15.014436.10060@kcbbs.gen.nz> Chris_Sullivan@kcbbs.gen.nz (Chris Sullivan) writes: > > class C { > > const int size = 1000; > > char array[size]; > > ... > > } > > This is a hack, but it has been suggested by greater authorities than me! > > class C { > enum { size = 1000 }; > char array[size]; > // ... > }; > > -- > Glenn P. Parker glenn@bitstream.com Bitstream, Inc. > uunet!huxley!glenn 215 First Street > BIX: parker Cambridge, MA 02142-1270 This following seems to work for our AT&T 2.0 derived compiler: class C { static const int size; char array[size]; // ... }; and then in the implementation file give the definition: const int C::size = 1000; Mike Burbidge MacApp Engineering
vaughan@puma.cad.mcc.com (Paul Vaughan) (06/17/91)
Curiously, as one of g++'s incompatabilities with Cfront and the
defacto standard, this is accepted with no errors.
class A {
public:
static const int size = 3;
char array[size];
};
I'm not sure just how well it works. Of course, Cfront says
[1.66]sunspot) !C
CC -c trash.cc
CC trash.cc:
"trash.cc", line 2: error: initializer for member size
"trash.cc", line 5: error: cannot evaluate constant
"trash.cc", line 5: error: dimension missing for array A::array
3 errors
Anyway, I wouldn't recommend using this, but it suggests a possible
solution to the problem assuming it could be implemented and didn't
interfere with anything else.
--
Paul Vaughan, MCC CAD Program | ARPA: vaughan@mcc.com | Phone: [512] 338-3639
Box 200195, Austin, TX 78720 | UUCP: ...!cs.utexas.edu!milano!cadillac!vaughan
----------------------------------------------------------------------------------
I spent from $3 to $10 today to pay interest on the national debt. How about you?
----------------------------------------------------------------------------------
gtodd@cix.compulink.co.uk (Giles Todd) (06/18/91)
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
glenn@bitstream.com (Glenn P. Parker) (06/18/91)
In article <1991Jun17.184324.1979@demon.co.uk> gtodd@cix.compulink.co.uk (Giles Todd) writes: > steve@taumet.com (Stephen Clamage) writes: > > 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. This would be *awful* if it were true. Fortunately, such paranoia is completely unjustified. A compiler [implementor] is allowed to represent a particular enum type with the smallest word-size necessary to represent *all* valid values for the enum. So, there should be absolutely no danger in having an enum type with a value outside the range 0..127. If your compiler fails this test, send it back. -- Glenn P. Parker glenn@bitstream.com Bitstream, Inc. uunet!huxley!glenn 215 First Street BIX: parker Cambridge, MA 02142-1270