views:

455

answers:

3

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()
 {
 }
};
+3  A: 

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.

Ashwin
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
I have edited my reply. I had linked the wrong question. Please take a look at it again.
Ashwin
`std::vector` is not the same. It allocates memory on the heap.
Kirill V. Lyadvinsky
I want this to be initialized in a read-only region of storage. Will it be possible with std::vector?
Vadakkumpadath
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.
drhirsch
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
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
@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
+1  A: 

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
A: 

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.

lionbest
array from tr1 has iterator and standard methods like size/begin/end as well. No need to use boost for just one simple class.
Kirill V. Lyadvinsky
You right. But I use boost everywhere before any tr1 implementation.
lionbest