simple question about "static"
-
- I use a static data member of a class. But the following codes have an error and a warning, "C:\TestConst\TestConst.cpp(8) : error C2057: expected constant expression C:\TestConst\TestConst.cpp(8) : warning C4200: nonstandard extension used : zero-sized array in struct/union" - I do not know why. (Such are my codes.) #include "iostream.h" class a{ private: static const int s ; int i[s]; }; const int a::s=20; void main() { cout<
Hello I tried the following code using GNU C++ compiler on Linux [Debian 2.2] It compiled and ran fine #include "iostream.h" class a{ public: static const int s ; int i[s]; }; const int a::s=20; void main() { cout<
-
- I use a static data member of a class. But the following codes have an error and a warning, "C:\TestConst\TestConst.cpp(8) : error C2057: expected constant expression C:\TestConst\TestConst.cpp(8) : warning C4200: nonstandard extension used : zero-sized array in struct/union" - I do not know why. (Such are my codes.) #include "iostream.h" class a{ private: static const int s ; int i[s]; }; const int a::s=20; void main() { cout<
-
You have used integer array as an data member in the class.Memory is allocated to the array at compile time itself.So arrays always want constant expression.So it gives an error. Hope this helps....:)
Since the variable is a constant the compiler knows the value at compile time. Therefore it's ok to use a const variable to declare a non-dynamic array. Anyway, here is what you can do...
#include "iostream.h"
class ClassA
{
private:
const static enum { s = 20 };
int i[s];public:
int Func(int n);
static int StaticFunc(int n);};
int ClassA::Func(int n)
{
return ClassA::s + n;
}int ClassA::StaticFunc(int n)
{
return ClassA::s + n;
}void main()
{
cout << sizeof(int) << endl;
cout << sizeof(ClassA) << endl;cout << ClassA::StaticFunc(1) << endl; ClassA a; cout << a.Func(2) << endl;
}
It's not necessary to specify ClassA:: before the variable name (s) in the class member functions, but I like it this way :) Sprudling
-
You have used integer array as an data member in the class.Memory is allocated to the array at compile time itself.So arrays always want constant expression.So it gives an error. Hope this helps....:)
Actually memory for the array isn't allocated at all since the class is never instanciated. But the forward reference for the value of s does make it impossible for the compiler to lay out the class structure fully until the reference is satisfied. It's just being a lazy compiler.
-
Hello I tried the following code using GNU C++ compiler on Linux [Debian 2.2] It compiled and ran fine #include "iostream.h" class a{ public: static const int s ; int i[s]; }; const int a::s=20; void main() { cout<
-
You have used integer array as an data member in the class.Memory is allocated to the array at compile time itself.So arrays always want constant expression.So it gives an error. Hope this helps....:)
-
Since the variable is a constant the compiler knows the value at compile time. Therefore it's ok to use a const variable to declare a non-dynamic array. Anyway, here is what you can do...
#include "iostream.h"
class ClassA
{
private:
const static enum { s = 20 };
int i[s];public:
int Func(int n);
static int StaticFunc(int n);};
int ClassA::Func(int n)
{
return ClassA::s + n;
}int ClassA::StaticFunc(int n)
{
return ClassA::s + n;
}void main()
{
cout << sizeof(int) << endl;
cout << sizeof(ClassA) << endl;cout << ClassA::StaticFunc(1) << endl; ClassA a; cout << a.Func(2) << endl;
}
It's not necessary to specify ClassA:: before the variable name (s) in the class member functions, but I like it this way :) Sprudling
-
Actually memory for the array isn't allocated at all since the class is never instanciated. But the forward reference for the value of s does make it impossible for the compiler to lay out the class structure fully until the reference is satisfied. It's just being a lazy compiler.
-
Maybe the compiler is trying to allocate memory for i[s] before it reachs the const a::s=20; line, causing an error because s would equal 0. Hope this helps - X
-
- Thanks pal! - Your reply helps a lot. I still have a question. Another pal says, "the variable is a constant the compiler knows the value at compile time". I do not know what is meaning. - Can you help? - Regards, Maer
I know constants in VB are known at compile time, but these are equivalent to #define's in VC. The const keyword means that the variable cannot be altered during the execution of the program. - X
-
I know constants in VB are known at compile time, but these are equivalent to #define's in VC. The const keyword means that the variable cannot be altered during the execution of the program. - X