Why does this variable need to be static?

Armon Safai
class armon
{
    static const int maxSize=10;    

    int array[maxSize];

    int count=0;

    int* topOfStack=array;
}

Why does maxSize need to be static for it to be used inside array?

Abhijit

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,

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why does a variable need to be static in the finally-block

From Dev

Why ThreadLocal variable need to static?

From Dev

Why does this static bool not need to be initialized?

From Dev

Why does the ClassCleanup Attribute need to be on a static method?

From Dev

Why static final variable use static method initialization need synchronized?

From Dev

Why static final variable use static method initialization need synchronized?

From Dev

Java : Why does placement of static variable matter?

From Dev

Why do you need to specify type of extern/ static variable at initialization?

From Dev

Why does lambda translation need generation of a static method?

From Dev

Why does a const reference to volatile int need a static_cast?

From Dev

Why does static member initialization need to be "typed again"?

From Dev

Why does a static data member need to be defined outside of the class?

From Dev

Why does inline function need to be declared static if it uses fprintf?

From Dev

Why does the nested class in a ConfigurationProperties class need to be static?

From Dev

Why does a router need to know the modem's static IP address?

From Dev

Why does the static modifier prevent its variable to be reassigned with a new value?

From Dev

CppUnit: Why does a static local variable keep its value?

From Dev

std::condition_variable why does it need a std::mutex

From Dev

Scala: Why does function need type variable in front?

From Dev

Why does a condition variable need a lock (and therefore also a mutex)

From Dev

why @autowired in spring does not need setter method for private instance variable?

From Dev

Why does importStatic Eclipse template variable need a namespace?

From Dev

Why does bash need && to echo a variable on one line?

From Dev

std::condition_variable why does it need a std::mutex

From Dev

Why does a static variable initialized by a method call that returns another static variable remains null?

From Dev

Assigning a static variable to another static variable. Why does it throw an error?

From Dev

Why do we need to define a static variable of a class, unlike the other member of a class?

From Dev

Why does we need access modifier "static" for some methods like the following one?

From Dev

Why does an oracle plsql varchar2 variable need a size but a parameter does not?

Related Related

  1. 1

    Why does a variable need to be static in the finally-block

  2. 2

    Why ThreadLocal variable need to static?

  3. 3

    Why does this static bool not need to be initialized?

  4. 4

    Why does the ClassCleanup Attribute need to be on a static method?

  5. 5

    Why static final variable use static method initialization need synchronized?

  6. 6

    Why static final variable use static method initialization need synchronized?

  7. 7

    Java : Why does placement of static variable matter?

  8. 8

    Why do you need to specify type of extern/ static variable at initialization?

  9. 9

    Why does lambda translation need generation of a static method?

  10. 10

    Why does a const reference to volatile int need a static_cast?

  11. 11

    Why does static member initialization need to be "typed again"?

  12. 12

    Why does a static data member need to be defined outside of the class?

  13. 13

    Why does inline function need to be declared static if it uses fprintf?

  14. 14

    Why does the nested class in a ConfigurationProperties class need to be static?

  15. 15

    Why does a router need to know the modem's static IP address?

  16. 16

    Why does the static modifier prevent its variable to be reassigned with a new value?

  17. 17

    CppUnit: Why does a static local variable keep its value?

  18. 18

    std::condition_variable why does it need a std::mutex

  19. 19

    Scala: Why does function need type variable in front?

  20. 20

    Why does a condition variable need a lock (and therefore also a mutex)

  21. 21

    why @autowired in spring does not need setter method for private instance variable?

  22. 22

    Why does importStatic Eclipse template variable need a namespace?

  23. 23

    Why does bash need && to echo a variable on one line?

  24. 24

    std::condition_variable why does it need a std::mutex

  25. 25

    Why does a static variable initialized by a method call that returns another static variable remains null?

  26. 26

    Assigning a static variable to another static variable. Why does it throw an error?

  27. 27

    Why do we need to define a static variable of a class, unlike the other member of a class?

  28. 28

    Why does we need access modifier "static" for some methods like the following one?

  29. 29

    Why does an oracle plsql varchar2 variable need a size but a parameter does not?

HotTag

Archive