Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Static attribute o QtClass
QtWS25 Last Chance

Static attribute o QtClass

Scheduled Pinned Locked Moved C++ Gurus
4 Posts 2 Posters 1.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dcbasso
    wrote on last edited by
    #1

    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]

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      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.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dcbasso
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • K Offline
          K Offline
          koahnig
          wrote on last edited by
          #4

          [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 ;-)

          Vote the answer(s) that helped you to solve your issue(s)

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved