Initializing arrays in a class

This is a discussion on Initializing arrays in a class within the C++ Programming forums, part of the General Programming Boards category; Hey, I have a class Foo, which has a member array of doubles, of length 5. This array is called ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    36

    Initializing arrays in a class

    Hey,

    I have a class Foo, which has a member array of doubles, of length 5. This array is called x. I want x to be a constant array, such that the values in the array are never changed, and I know what I want them to be at the start of my program. However, I don't seem to be able to do this in a concise manner. Here is what I tried :

    Code:
    class Foo
    {
    const double x[5] = {1, 2, 3, 4, 5};
    }
    This does not work. Nor does:

    Code:
    class Foo
    {
    const double x[5];
    
    public Foo()
    {
    x = {1, 2, 3, 4, 5,};
    }
    The only way that works is:

    Code:
    class Foo
    {
    const double x[5];
    
    public Foo()
    {
    x[0] = 1;
    x[1] = 2;
    x[2] = 3;
    x[3] = 4;
    x[4] = 5;
    }
    }
    But this obviously becomes a very long-winded approach when using larger arrays.

    Please could somebody explain what is going on here?

    Thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,306
    In this case it probably makes sense to do this:
    Code:
    class Foo
    {
        static const double x[5];
    public:
        // ...
    };
    Then in the source file:
    Code:
    const double Foo::x[5] = {1, 2, 3, 4, 5};
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Basically I don't think it is possible to initialize arrays in classes. If that is so it would be impossible to have a const array per instance. (You can't assign the values one by one either.)

    On the other hand, there is no reason why each instance of this class should get their own immutable array. You can declare it static, meaning that it will be shared between all the instances and won't increase the size of the instances.

    Code:
    class Foo
    {
    static const double x[5];
    
    };
    
    const double Foo::x[5] = {1, 2, 3, 4, 5};
    On the other hand, with boost you can also do things like this:

    Code:
    #include <boost/array.hpp>
    #include <boost/assign.hpp>
    
    class Foo
    {
        const boost::array<int, 5> x;
    public:
        Foo():
            x(boost::assign::list_of(1)(2)(3)(4)(5))
        {
        }
    };
    Although I can't still see any point why you would want each instance to have their own copy of the array if it contains the same immutable values.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Initializing a pointer in a class.
    By gpr1me in forum C++ Programming
    Replies: 3
    Last Post: 03-22-2006, 03:05 PM
  2. Replies: 4
    Last Post: 04-02-2004, 07:30 PM
  3. Replies: 1
    Last Post: 04-20-2003, 06:02 PM
  4. arrays as class names
    By Katle in forum C++ Programming
    Replies: 2
    Last Post: 11-25-2002, 03:58 PM
  5. Do you store store one off data arrays in a class?
    By blood.angel in forum C++ Programming
    Replies: 5
    Last Post: 06-24-2002, 01:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21