Connecting Tech Pros Worldwide Help | Site Map

How to initialize an array member in the member initialization list?

jut_bit_zx@eyou.com
Guest
 
Posts: n/a
#1: Oct 9 '05
class A
{
public:
A();
virtual ~A(){}
....

private:
int m_iarray[10];
}

How can I initialize "m_iarray" int the member initialization list? If
cann't, then why?

Jacques Labuschagne
Guest
 
Posts: n/a
#2: Oct 9 '05

re: How to initialize an array member in the member initialization list?


jut_bit_zx@eyou.com wrote:
[color=blue]
> class A
> {
> public:
> A();
> virtual ~A(){}
> ...
>
> private:
> int m_iarray[10];
> }
>
> How can I initialize "m_iarray" int the member initialization list? If
> cann't, then why?
>[/color]

You can't, because there's no supported syntax. :-) Who knows, there
might even be a compelling technical reason.
You could use vectors with the "vector::vector(size_type n, const T&
value = T())" constructor, but that doesn't help if you wanted the
elements to have different values.

Jacques.
Alf P. Steinbach
Guest
 
Posts: n/a
#3: Oct 9 '05

re: How to initialize an array member in the member initialization list?


* Jacques Labuschagne:[color=blue]
> jut_bit_zx@eyou.com wrote:
>[color=green]
> > class A
> > {
> > public:
> > A();
> > virtual ~A(){}
> > ...
> >
> > private:
> > int m_iarray[10];
> > }
> >
> > How can I initialize "m_iarray" int the member initialization list? If
> > cann't, then why?
> >[/color]
>
> You can't, because there's no supported syntax. :-) Who knows, there
> might even be a compelling technical reason.
> You could use vectors with the "vector::vector(size_type n, const T&
> value = T())" constructor, but that doesn't help if you wanted the
> elements to have different values.[/color]

An array can be default-initialized, which for this array means
zero-initialized, via the constructor initializer list.

However, MSVC 7.1 doesn't support that; it compiles but doesn't give the
initialization it should.

I suspect there are also other commonly used compilers that don't support it.

--
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?
Peter_Julian
Guest
 
Posts: n/a
#4: Oct 10 '05

re: How to initialize an array member in the member initialization list?



<jut_bit_zx@eyou.com> wrote in message
news:1128837550.509154.262100@f14g2000cwb.googlegr oups.com...
| class A
| {
| public:
| A();
| virtual ~A(){}
| ...
|
| private:
| int m_iarray[10];
| }
|
| How can I initialize "m_iarray" int the member initialization list? If
| cann't, then why?
|

To initialize the array to any specific value, you have to wrap its
element type and provide your own default ctor.

Why? Because arrays are primitive containers that adhere to a couple of
old rules:
a) they must be initialized to a known constant size
b) each element of the array must have a default ctor available and
invokeable at birth.

Suppose that we replaced the int type above with a simple struct N. You
would not be able to generate an instance of class A if type N did not
have a default ctor available. Try it, comment the default ctor below...

// test_array.cpp
#include <iostream>

struct N
{
int m_n;
N() : m_n(0) { std::cout << "N() "; } // def ctor
N(int n) : m_n(n) { std::cout << "N(int n) "; }
~N() { std::cout << "~N() "; }
};

class A
{
N m_array[10];
public:
A() { std::cout << "A()\n"; }
~A() { std::cout << "\n~A() "; }
};

int main()
{
A a;

return 0;
}

/*
N() N() N() N() N() N() N() N() N() N() A()

~A() ~N() ~N() ~N() ~N() ~N() ~N() ~N() ~N() ~N() ~N()

*/
______

If this limitation of the array is an issue, consider the std::vector. A
much more flexible, dynamic and capable container, not to mention much,
much easier to use.
______
// test_vector.cpp
#include <iostream>
#include <vector>
#include <algorithm>

template < class T >
class V
{
std::vector< T > m_v;
public:
V(int size, T value) : m_v(size, value) { }
~V() { }
void display() const
{
std::cout << std::endl;
std::copy( m_v.begin(),
m_v.end(),
std::ostream_iterator< T >(std::cout, " ") );
}
};

int main()
{
// generate an instance of usertype V with
// a vector of 10 int elements all initialized to 1
V<int> v(10, 1);
v.display();

// a vector of 10 doubles initialized to 9.9
V<double> vd(10, 9.9);
vd.display();

return 0;
}

/*
1 1 1 1 1 1 1 1 1 1
9.9 9.9 9.9 9.9 9.9 9.9 9.9 9.9 9.9 9.9
*/



Closed Thread