Static attribute o QtClass
-
Hello everyone, I'm trying to put the static attribute on MyClass, but the compiler gives me an error.
I try this:
@
//MyClass.h
static int INDEX_MCI;
static int INDEX_MCI = 0;
@How can I make some static attrs. on my classes?
Thanks.[Moved to C++ Gurus -- mlong]
-
Well, that is not really an expert question and it has nothing to do with Qt.
For your example above:
@
//MyClass.h
// static int INDEX_MCI;
static int INDEX_MCI = 0;
@
However, you should include the file only once, I guess.
BTW your example is not very meaningful.Probably you like to do something like this:
a.h
@
class A
{
static const int INDEX_MCI;
public:
A() {};
};
@
You need to do the initialization in the source file:
a.cpp:
@
#include "a.h"const int A::INDEX_MCI = 0;
...
@If you do not want to have a const instance, you can basically initialize anywhere in your code. However, you should not twice an initiation. Remember static does not mean that the instance is constant, it is simply shared among different modules or instances.
-