Initialize an array in the constructor initialization list

I am trying to initialize an array in the constructor initialization list, and I want the array to have a size of MAX_SIZE, which is an open static const in my Stack class. How can I make it work? The compiler complains, saying that they have incompatible types when assigning 'double' to 'double [0u]'

Here is my code:

class Stack {
    public:      
          Stack();
          static const unsigned MAX_SIZE; 
    private:
          double array[];
          unsigned elements;     
    };  // class Stack

    Stack::Stack(): array( array[MAX_SIZE] ), elements(0) {}

    const unsigned Stack::MAX_SIZE = 4;

Thanks in advance for your help.

+5
source share
1 answer
 class Stack {
        public:
              Stack();
              static const unsigned MAX_SIZE = 4;
        private:
              double array[MAX_SIZE];
              unsigned elements;
        };  // class Stack

 Stack::Stack():  array(), elements(0) {}

But, std::vectorit would be better, as indicated in the comments.

+5

All Articles