Init an array in a class

Discussion in 'C++' started by Vij, Sep 6, 2005.

  1. Vij

    Vij Guest

    I can do this
    int a [] = { 5,6,7,8,9};

    but how can I do this inside a class? something like this

    class CTest
    {
    private:
    int a [] // Init the array here
    };
     
    Vij, Sep 6, 2005
    #1
    1. Advertising

  2. Vij

    Gabriel Guest

    Vij wrote:
    > I can do this
    > int a [] = { 5,6,7,8,9};
    >
    > but how can I do this inside a class? something like this
    >
    > class CTest
    > {
    > private:
    > int a [] // Init the array here
    > };
    >

    I would do it like this:
    // header file
    #include <vector>
    class CTest
    {
    public:
    CTest();
    private:
    std::vector<int> a;
    }

    //source file
    namespace{
    // solution 1 for short arrays:
    std::vector<int> init_a()
    {
    std::vector<int> result(5);
    a.at(0) = 5;
    //...
    a.at(4) = 9;
    return result;
    }

    // solution 2 for long array:
    std::vector<int> init_a()
    {
    const int a [] = {5, 6, 7, 8, 9};
    return std::vector<int>(a, &a[5]); // You might have been looking for
    this? vector constructor can take two iterators.
    // or, to be very safe:
    // return std::vector<int>(a, &a[sizeof(a) / sizeof(int) - 1]);
    }

    } // unnamed namespace

    CTest()
    : a(init_a())
    {}

    - Gabriel
     
    Gabriel, Sep 6, 2005
    #2
    1. Advertising

  3. On Tue, 06 Sep 2005 15:30:24 +0400, Vij <> wrote:

    > I can do this
    > int a [] = { 5,6,7,8,9};
    > but how can I do this inside a class? something like this
    > class CTest
    > {
    > private:
    > int a [] // Init the array here
    > };


    You can't specify explicit initializers for arrays in constructor's
    initialization list nor in class declration. You can initialize array as
    described before or use static arrays:

    struct A
    {
    static int a[];
    };

    int A::a[] = {1,2,3};

    void main() {
    A x;
    }
     
    Manvel Avetisian, Sep 6, 2005
    #3
  4. Vij

    Rolf Magnus Guest

    Vij wrote:

    > I can do this
    > int a [] = { 5,6,7,8,9};
    >
    > but how can I do this inside a class?


    You can't.

    > something like this
    >
    > class CTest
    > {
    > private:
    > int a [] // Init the array here
    > };


    An array needs a size. In the first example, the size can be deduced from
    the initializer. But in a class, you can't put an initializer here, so you
    must specify a size. Further, non-static array members cannot be
    initialized at all in C++.
    So the only thing you can do is explicitly specify a size for the array and
    assign all the elements in the constructor, like:

    class CTest
    {
    public:
    CTest()
    {
    a[0] = 5;
    a[1] = 6;
    a[2] = 7;
    a[3] = 8;
    a[4] = 9;
    private:
    int a [5];
    };
     
    Rolf Magnus, Sep 6, 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. Tony Cheng
    Replies:
    1
    Views:
    8,221
    Juan T. Llibre
    Feb 24, 2006
  2. Victor Hannak
    Replies:
    5
    Views:
    554
    Victor Hannak
    Sep 26, 2003
  3. Replies:
    1
    Views:
    666
    Jules
    Aug 18, 2005
  4. Jess
    Replies:
    4
    Views:
    445
  5. news.aon.at
    Replies:
    11
    Views:
    651
    Ian Collins
    Jan 29, 2011
Loading...

Share This Page