Why does this variable need to be static? Why does this variable need to be static? arrays arrays

Why does this variable need to be static?


It doesn't have to be static, but it must be a constant expression.

C++ Standard § 8.3.4 [dcl.array] (emphasis mine) :

If the constant-expression (5.19) is present, it shall be a converted constant expression of type std::size_t and its value shall be greater than zero


That is, the following is also valid :

constexpr std::size_t Size()  { return 10; }; struct Y{    int array[Size()];};

Note:

Since the compiler needs to know the size of the class, you cannot do this :

struct Y{    const int size = 10;    int array[size];};

Possibly making different instances of Y having different sizes.

Also note that in this context, int array[size] is not a constant expression, because it makes use of this, see the C++ standard section § 5.19 [expr.const] :

A conditional-expression e is a core constant expression unless the evaluation of e, following the rules of the abstract machine (1.9), would evaluate one of the following expressions:

this (5.1.1), except in a constexpr function or a constexpr constructor that is being evaluated as part of e;

(the evaluation of size is really this->size)


There are two aspects to this question

Aspect 1

C++ array is of fixed size, the size of which needs to be known during compile time. If the decision needs to be deferred during runtime, the array expression becomes ill-formed.

Aspect 2

Declaring a member variable as non-static makes it an instance variable, the value of which only exist once the object is instantiated which is done during run-time. A static variable is a class variable, the value of which can be determined during compile time.

Your particular example becomes the classic chicken-egg paradox.

class armon{    static const int maxSize=10;        int array[maxSize];}
  • In order to instantiate your class armon, you need to know its size.
  • In order to know its size, you need to know the size of individual members. In your particular case, you need to know the size of the array.
  • In order to know the size of the array, you need to know the value of the dependent variable maxSize.
  • In order to access the dependent variable maxSize you need to instantiate the class armon.
  • In order to instantiate your class armon, you need to know its size.

So, your array size dependent variable should be a constant expression, which in your particular case should be a static variable,


It doesn't have to be static, it has to be constant.

When you declare a constant inside a class you will be making a constant for each instance of the class.

Also if your maxSize is just const you would have to intialize it in the constructor initializer list because const maxSize is treated as variable whose value you can't change.

Inside a class constkeyword means "This is a constant during the hole lifetime of this object". Different objects of the same class can have different values for that constant.

But when it is a static constant there will be only one constant for all instances of the class. This means that you have to initialize constants value on the same line where you are defining it.