[comp.lang.c++] consts in classes

preston@peritus.UUCP (Preston Gardner) (11/10/88)

Is it possible to define a const inside of a class?  I tried to do it in
various ways, but I could never work out a way to initialize it.
(Const objects without initialization are useless in C++ and cfront
squawks if it sees them.  Cf. ANSI consts, which can be uninitialized,
e.g. a const volatile object that refers to a read-only I/O register.
The ANSI idea of const is different.)

Suppose I want to do this:

	class XType {
	    const int MAX = 20;  // I don't like the C preprocessor define
	    int array[MAX];

	    void XYZ() { // uses array and MAX somehow 
	    };
	};

I can't really get it in C++.  I don't know how to get the definition of
MAX confined within the scope of XType, except to use #define and #undef.


Another question:  Suppose that it is possible to have const objects inside
of a class.  Is there one per object, or one for the whole type?  If there
is one for the whole type then they would be a lot like static class
fields.
-- 
				-- Preston Gardner
				   Peritus International, Inc.
				   ..{pyramid,sun}!oliveb!peritus!preston

nevin1@ihlpb.ATT.COM (Liber) (11/11/88)

In article <155@peritus.UUCP> preston@peritus.UUCP (Preston Gardner) writes:
|Is it possible to define a const inside of a class? 

I think so.

|Suppose I want to do this:

|	class XType {
|	    const int MAX = 20;  // I don't like the C preprocessor define
|	    int array[MAX];
|
|	    void XYZ() { // uses array and MAX somehow 
|	    };
|	};

Declaring MAX as a const static int should work.

|Another question:  Is there one per object, or one for the whole type?

Since it is declared as static, there is only one for the whole type.
Just wondering:  are there any situations where it would be useful to
have a separate constant per object, instead of just having one for the
whole class (and would initialization then be dependent on
#ifdef/#endif in the library files -- very ugly IMHO)?

|If there is one for the whole type then they would be a lot like static
|class fields.

That is exactly what they are.


Hope this helps (and is correct; I'm a bit sleepy right now and my
reference material is in the trunk of my car),
-- 
 _ __		NEVIN J. LIBER  ..!att!ihlpb!nevin1  (312) 979-4751  IH 4F-410
' )  )  "I catch him with a left hook. He eels over. It was a fluke, but there
 /  / _ , __o  ____  he was, lying on the deck, flat as a mackerel - kelpless!"
/  (_</_\/ <__/ / <_	As far as I know, these are NOT the opinions of AT&T.

vjm@endor.harvard.edu (Victor Milenkovic) (11/17/88)

The question was raised whether constant class members are useful,
whether they vary from instance to instance, and how they can be
initialized.

Constant members that vary from instance to instance exist are useful.
For example your name and social security number are constants that
differ from those of other instances of class "human".  See page 297
(Section 10. Function Definitions) of Stroustrup's book for the
appropriate initialization syntax.

Victor Milenkovic
Harvard University
vjm@endor.harvard.edu