2

How can we declare a non-static const array as an attribute to class?

Following code produces compilation error

'Test::x' : member could not be initialized

class Test
{
public:
    const int x[10];

public:
    Test()
    {
    }
};
Weak to Enuma Elish
  • 4,622
  • 3
  • 24
  • 36
Vadakkumpadath
  • 1,445
  • 13
  • 21
  • I need to store some configuration data which is available at the time of compilation. I want this to be placed in a read-only region of storage. – Vadakkumpadath Oct 05 '09 at 10:35

3 Answers3

3

You should read this already posted question. Since it is not possible to do what you want, the workaround is to use an std::vector.

Community
  • 1
  • 1
Ashwin
  • 3,609
  • 2
  • 18
  • 11
  • Thaks for reply. But that solution is not possible in case of arrays. When we are initializing arrays, we will get another compilation error ("cannot specify explicit initializer for arrays"). – Vadakkumpadath Oct 05 '09 at 09:23
  • I have edited my reply. I had linked the wrong question. Please take a look at it again. – Ashwin Oct 05 '09 at 09:33
  • `std::vector` is not the same. It allocates memory on the heap. – Kirill V. Lyadvinsky Oct 05 '09 at 10:07
  • I want this to be initialized in a read-only region of storage. Will it be possible with std::vector? – Vadakkumpadath Oct 05 '09 at 10:37
  • 1
    You wan't a dynamically allocated object which allocates memory in a read only region. This is not possible, the memory in the read-only region needs to be initialized at compile time, but at compile time it is not known how many objects you will create. – Gunther Piez Oct 05 '09 at 10:51
  • Actually I don't need to allocate dynamically. The data to be initialized is available at the time of compilation. A declaration "const Test T = {0}" is possible when there is no constructor defined for this class. I need this kind (or something like this) of initialization with a defined constructor. – Vadakkumpadath Oct 05 '09 at 12:30
  • If you don't want it dynamically allocated and you want it to point to something in ROM that's initialized at compile time why not just have a static const member array? You'll be able to do exactly what you want. – Michael Burr Oct 06 '09 at 04:22
  • @Michael - Thanks, But I need to create some (say 5) instances of this class having different configurations (Configuration can be stored in x[]). So I can't use a static array. – Vadakkumpadath Oct 06 '09 at 05:06
1

You could use array class from tr1.

class Test
{
public:
 const array<int, 10> x;

public:
 Test(array<int,10> val) : x(val) // the only place to initialize x since it is const
 {
 }
};

array class could be simplistically represented as follows:

template<typename T, int S>
class array
{
    T ar[S];
public:
    // constructors and operators
};
Kirill V. Lyadvinsky
  • 97,037
  • 24
  • 136
  • 212
0

Using boost::array (the same as tr1) it will looks like:

    #include<boost/array.hpp>

    class Test
    {   
       public:

        Test():constArray(staticConst) {}; 
        Test( boost::array<int,4> const& copyThisArray):constArray(copyThisArray) {}; 

        static const boost::array<int,4> staticConst; 

        const boost::array<int,4> constArray;
    };

    const boost::array<int,4> Test::staticConst = { { 1, 2, 3 ,5 } };

The extra code static member is needed because { { 1, 2, 3 ,5 } } is invalid in initialization list.

Some advantages is that boost::array have defined iterator and standard container methods like size, begin and end.

Arpegius
  • 5,817
  • 38
  • 53