Re: Initialization of a const POD member
* Alexander Arhipenko:
Guys, I have a question related to the one submitted by Pierre.
I have following class:
class PermArray
{
enum { size = 11 };
size_t array_[size];
public:
PermArray()
{}
size_t* begin() { return array_; }
size_t* end() { return array_ + size; }
};
array_ should always have values {0,1,2,3...,10} after PermArray
object is constructed (PermArray means here array for permutations). I
don't want to call std::generate algo each time since I know the
initial values of array_ at compile time. I would like to use solution
like:
class PermArray
{
//...
static const size_t n_array_[size];
};
const size_t PermArray::n_array_[size] = {0,1,2,3,4,5,6,7,8,9,10};
PermArray::PermArray() : array_(n_array_)
{}
But this won't compile since compiler "thinks" that I'm trying to
initialize pointer.
<code>
#include <cstddef>
using std::size_t;
template< typename T, size_t N >
struct RawArray
{
T elem[N];
};
class PermArray
{
private:
enum { size = 11 };
RawArray<size_t, size> array_;
static const RawArray<size_t, size> n_array_;
public:
PermArray(): array_( n_array_ )
{}
size_t* begin() { return array_.elem; }
size_t* end() { return array_.elem + size; }
};
RawArray<size_t, PermArray::size> const PermArray::n_array_ =
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
</code>
Check out the Boost library for a reusable wrapper like RawArray, so
that you don't have to reinvent the wheel again and again.
I'm assuming there is such a wrapper.
Cheers, & hth.,
- Alf
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]