Topic: Aggregate initialization of const array data members


Author: rkotar1@umbc.edu (kotaru rajashekar)
Date: 1997/08/19
Raw View
C++ compilers enforce the const-ness of data members by allowing
their one-time initialization in the initialization list of the
class constructor.

If a class has a const array data member, how does one initialize
it?

For example:


class A
{
public:
 A(void);
private:
 const double a[2][3];
};

double b[2][3] = { {1, 2, 3}, {4, 5, 6} }; // extern with file scope

A::A(void)
 : a(b) // Error: member A::a cannot be initialized
{
}

A::A(void)
 : a( {{1.,2.,3.}, {4., 5. 6.}}) // Error: member A::a cannot be initialized
{
}

Both versions of the ctor fail to compile with the error message
shown.   Short of doing an element by element initialization of
the array, is there any work-around?

Thanks
Raj
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: Alexandre Oliva <oliva@dcc.unicamp.br>
Date: 1997/08/20
Raw View
kotaru rajashekar writes:

> If a class has a const array data member, how does one initialize
> it?

There's no way to provide a non-default initialization.  You'll have
to use an STL vector and initialize it with a range of iterators or
copy-initialize it.

--
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:aoliva@acm.org
Universidade Estadual de Campinas, SP, Brasil
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: Steve Clamage <stephen.clamage@eng.sun.com>
Date: 1997/08/21
Raw View
kotaru rajashekar wrote:
>
> If a class has a const array data member, how does one initialize it?
>
> For example:
>
> class A
> {
> public:
>         A(void);
> private:
>         const double a[2][3];
> };

No syntax is available to intialize the array.

I can think of several alternatives:

1. Don't make the array const. Since it is private, it probably
doesn't need to be const. (Only member functions can access
it, and you have control over them.) Fill the array in the
body of the constructor.

2. Make the array static. If you need different contents in
different objects, that isn't suitable, of course.

3. Don't put the array in the class. Put a pointer-to-const
in the class, and point to a static or dynamic array that the
constructor creates. That allows per-instance array contents.

4. Use an array class of some kind instead of a C-style array.

--
Steve Clamage, stephen.clamage@eng.sun.com
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]