Thread: Initializing arrays in a class

  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
    28,413
    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};
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 05: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, 12:05 PM