Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

initializing const type array member

1 view
Skip to first unread message

Paul Lutus

unread,
Mar 6, 2000, 3:00:00 AM3/6/00
to
> Short answer: you can't. In fact, you can't initialize any
> array in a class.

Correction: s/array/non-static array/

And, if you explicitly initialize it in the constructor, even a non-static
array can be initialized.

--

Paul Lutus
www.arachnoid.com


Daniel Jones <ddjo...@SPAMspeakeasy.org> wrote in message
news:rlv7csk29f2uhbigk...@4ax.com...
> On Sat, 04 Mar 2000 16:48:51 GMT, in
> alt.comp.lang.learn.c-c++ you wrote:
>
> >I was wondering if anybody knows how to initialize a const type array
> >as a member of a class/struct?
> >
> >i.e.
> >
> >typedef int Type;
> >
> >struct X {
> > const Type Arr[ 10 ];
> >};
> >
>
> Short answer: you can't. In fact, you can't initialize any
> array in a class. C++ doesn't provide a syntax for doing
> so. You have to explicitly set each member individually
> inside the constructor.
>
> For a const array, you must use a const_cast in the
> constructor. Something like this:
>
> class MyClass
> {
> private:
> const int CI_Array[3];
> public:
> MyClass();
> };
>
> MyClass::MyClass()
> {
> int *i = const_cast<int *>(CI_Array);
> i[0] = 1;
> i[1] = 7;
> file://etc
> }
>
> Ugly but it will work.
>
>

Paul Lutus

unread,
Mar 6, 2000, 3:00:00 AM3/6/00
to
> To my understanding, this is not an
> initialization. It is assignment.

If it takes place before the first use of the array, in particular if it
takes place in the class constructor, this is more semantic than
substantive. How would you describe the actions meant to prepare an array
for use, and meaningfully avoid "initialization" or a synonym?

> Otherwise, I stand by my statement that one can not initialize an array
class member.

All this aside, you still would need to qualify the statement to exclude
static arrays.

--

Paul Lutus
www.arachnoid.com


Daniel Jones <ddjo...@SPAMspeakeasy.org> wrote in message

news:9ld8cs0d1h56klhd0...@4ax.com...


> On Mon, 06 Mar 2000 22:04:20 GMT, "Paul Lutus"
> <nos...@nosite.com> wrote:
>
> >> Short answer: you can't. In fact, you can't initialize any
> >> array in a class.
> >Correction: s/array/non-static array/
> >And, if you explicitly initialize it in the constructor, even a
non-static
> >array can be initialized.
>

> I assume you're using a different meaning for the term
> "initialize". My understanding of the term is this:
>
> To initialize an array, one uses this syntax:
> int IArray[5] = {1, 2, 3, 4, 5};
>
> To initialize a class member, one uses this syntax:
> class MyClass
> {
> protected:
> int i;
> char c;
> public:
> MyClass::MyClass();
> };
>
> MyClass::MyClass() : i(7), c('a')
> {
> //...
> }
>
> You can not initialize a class array member in this fashion:
>
> MyClass::MyClass() : IArray = {1, 2, 3, 4, 5} file://ERROR
>
> C++ does not provide a syntax for this. You must explicitly
> assign a value to each member of the array within the body
> of the constructor. To my understanding, this is not an
> initialization. It is assignment. Initialization takes
> place when one assigns an initial value at the time of
> declaration or uses the class initialization syntax shown
> above.
>
> If you can show me that I'm using the term incorrectly, I'd
> appreciate it. I'm always happy to have my
> misunderstandings corrected. Alternatively, you can show me
> the correct syntax for initializing an array. Otherwise, I
> stand by my statement that one can not initialize an array
> class member. This is true regardless of whether the array
> is static, const, neither or both. Normally, a const array
> is initialized as it can not be assigned. Since this is
> impossible for a class member array, the only way to assign
> a value to a const array member is to cast away the const
> inside the constructor.

0 new messages