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]


  • Moderators

    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.



  • Sorry guys, a very dummy question but sometimes is hard to find a simple explanation like that.
    Thank you very much koahnig, you solve my problem.


  • Moderators

    [quote author="dcbasso" date="1341349280"]Sorry guys, a very dummy question but sometimes is hard to find a simple explanation like that.
    Thank you very much koahnig, you solve my problem.[/quote]
    Well, this happens to all of us, once in a while ;-)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.