Thread: how to initialize static const array member of a class

  1. #1
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463

    how to initialize static const array member of a class

    Hi,
    I found myself in this situation and my reference book doesn't give a good answer and searching on google has yield not so desirable result. So I have an array that every object of the class will use, so I want to make that array "static const", so every object of the same class doesn't have to construct that array and these objects can't alter any part of the array.

    One of the answer is only shows how to initialize a static array:

    Code:
    // example of static array private member initialization
    class A
    {
    private:
        static int array[20] =;
    public:
        static void setArray();
    ...
    };
    
    int A::array[] = {0};
    
    void A::setArray()
    {
    for(int i = 0; i < 20; i++)
       array[i] = i;
    }
    This code doesn't work for "static const int array[20]". The compiler would protest. And since this is of type "int array[]" and not an integral type, the follow method would not work either:

    Code:
    class A
    {
    private:
        static const int array[20] = {1,2,3,...20};
    public:
    ...
    };
    This seems difficult because static variable can't be initialized in a constructor, and const variable has to be initialized in the constructor. And initialization of " static const" only works for integral types.
    Do any one knows a work-around? Or I just have to be careful and not alter alter any part of the static array?

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    By definition, a static const array member of a class can only be initialised when it is defined. That is typically before any constructors for the class are invoked.
    Try this;
    Code:
    class A
    {
    private:
        static const int array[20];
    public:
    
    };
    
    const int A::array[20] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19};
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. initialize member array in initializer list?
    By Vick jr in forum C++ Programming
    Replies: 4
    Last Post: 05-20-2010, 06:39 AM
  2. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  3. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM