[comp.lang.c++] Constant data tables and classes

jkw@genrad.com (J. Kyle Wilson) (03/30/91)

	I'm relatively new to C++, but have signifigant amounts of
experience with C.  It appears that C++ intentionally prohibits static
const tables within classes.  It would seem to me that allowing these
would provide a convenient way of hiding constant data tables used by
a class from outside use rather than using file scoped statics.  Does
anyoune know why this is disallowed?

						-Kyle Wilson (jkw@genrad.com)
						-GenRad Inc, Concord MA

bs@alice.att.com (Bjarne Stroustrup) (03/30/91)

 > 	I'm relatively new to C++, but have signifigant amounts of
 > experience with C.  It appears that C++ intentionally prohibits static
 > const tables within classes.  It would seem to me that allowing these
 > would provide a convenient way of hiding constant data tables used by
 > a class from outside use rather than using file scoped statics.  Does
 > anyoune know why this is disallowed?

Is this what you want?:

	class X {
		static const Table t;
		// ...
	};

	const Table X::t = { /* lots of stuff */ };

If so, it works.

roger@zuken.co.jp (Roger Meunier) (04/08/91)

In article <20146@alice.att.com> bs@alice.att.com (Bjarne Stroustrup) writes:

Kyle Wilson (jkw@genrad.com) wrote:
 >  >  It appears that C++ intentionally prohibits static
 >  > const tables within classes.  It would seem to me that allowing these
 >  > would provide a convenient way of hiding constant data tables used by
 >  > a class from outside use rather than using file scoped statics.  Does
 >  > anyoune know why this is disallowed?
 > 
 > Is this what you want?:
 > 
 > 	   class X {
 > 		   static const Table t;
 > 		   // ...
 > 	   };
 > 
 > 	   const Table X::t = { /* lots of stuff */ };
 > 
 > If so, it works.

How would you initialize the Table t for all instances in an array
of class X, as in the following?

X	instanceList[20];

I'm finding it very hard to encapsulate such static tables (arrays
of aggregates which I want to encapsulate in a C++ class) in C++.
Has anyone arrived at an "elegant" solution?
--
Roger Meunier @ Zuken, Inc.  Yokohama, Japan	(roger@zuken.co.jp)

steve@lia (Stephen Williams) (04/08/91)

In article <20146@alice.att.com> bs@alice.att.com (Bjarne Stroustrup) writes:
>
>
>
> > 	I'm relatively new to C++, but have signifigant amounts of
> > experience with C.  It appears that C++ intentionally prohibits static
> > const tables within classes.  It would seem to me that allowing these
> > would provide a convenient way of hiding constant data tables used by
> > a class from outside use rather than using file scoped statics.  Does
> > anyoune know why this is disallowed?
>
>Is this what you want?:
>
>	class X {
>		static const Table t;
>		// ...
>	};
>
>	const Table X::t = { /* lots of stuff */ };
>
>If so, it works.


The above works, but the following does not work in C++ 2.0:

	class X {

		int table[10];
		// ...
	};
	int X::table[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

This is quite frustrating.  However, it works fine in C++ 2.1. (Yeah!)

--Steve
steve@lia.com

mgates@entiat.boeing.com (Michael Gates) (04/09/91)

In article <ROGER.91Apr8115402@rd13.zuken.co.jp> roger@zuken.co.jp (Roger Meunier) writes:
>In article <20146@alice.att.com> bs@alice.att.com (Bjarne Stroustrup) writes:
>Kyle Wilson (jkw@genrad.com) wrote:
> >  >  It appears that C++ intentionally prohibits static
> >  > const tables within classes.  It would seem to me that allowing these
> >  > would provide a convenient way of hiding constant data tables used by
> >  > a class from outside use rather than using file scoped statics.  Does
> >  > anyoune know why this is disallowed?
> > 
> > Is this what you want?:
> > 
> > 	   class X {
> > 		   static const Table t;
> > 		   // ...
> > 	   };
> > 
> > 	   const Table X::t = { /* lots of stuff */ };
> > 
> > If so, it works.
>
>How would you initialize the Table t for all instances in an array
>of class X, as in the following?

Static class members are independent of instances, so there is only one
Table t for the class X, not one for each instance.
--
et tu mgates?