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

Discussion in 'C++' started by jut_bit_zx@eyou.com, Oct 9, 2005.

  1. Guest

    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?
    , Oct 9, 2005
    #1
    1. Advertising

  2. Re: How to initialize an array member in the member initializationlist?

    wrote:

    > 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?
    >


    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.
    Jacques Labuschagne, Oct 9, 2005
    #2
    1. Advertising

  3. Re: How to initialize an array member in the member initialization list?

    * Jacques Labuschagne:
    > wrote:
    >
    > > 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?
    > >

    >
    > 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.


    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?
    Alf P. Steinbach, Oct 9, 2005
    #3
  4. Peter_Julian Guest

    <> wrote in message
    news:...
    | 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::eek:stream_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
    */
    Peter_Julian, Oct 10, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Avner Flesch

    initialize an array member

    Avner Flesch, Nov 26, 2003, in forum: C++
    Replies:
    4
    Views:
    22,818
    EventHelix.com
    Nov 26, 2003
  2. Replies:
    6
    Views:
    451
    Ron Natalie
    Dec 11, 2005
  3. toton
    Replies:
    5
    Views:
    921
    Victor Bazarov
    Sep 28, 2006
  4. aaragon
    Replies:
    2
    Views:
    607
    James Kanze
    Nov 2, 2008
  5. Replies:
    30
    Views:
    2,022
    Triple-DES
    Mar 4, 2009
Loading...

Share This Page