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?
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];
}
armon
, you need to know its size.armon
.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.
Comments