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. Const in header

Const in header

Scheduled Pinned Locked Moved C++ Gurus
9 Posts 4 Posters 5.1k 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.
  • S Offline
    S Offline
    spode
    wrote on 7 Aug 2011, 10:09 last edited by
    #1

    how to declare a constant in header file? in THINKING IN C++ it is: "extern const NOME;"...
    ah, could i initializate the constant in header file?

    1 Reply Last reply
    0
    • L Offline
      L Offline
      loladiro
      wrote on 7 Aug 2011, 11:01 last edited by
      #2

      For an integer constant I would use
      @
      enum { CONSTANT = 123 }
      @ . For char *:
      @
      const char * const MY_CONSTANT = "ABC";
      @

      1 Reply Last reply
      0
      • A Offline
        A Offline
        alexisdm
        wrote on 7 Aug 2011, 11:02 last edited by
        #3

        Just remove the extern qualifier and add the initialization:
        @const char * NOME = "xxx";@

        1 Reply Last reply
        0
        • L Offline
          L Offline
          loladiro
          wrote on 7 Aug 2011, 11:06 last edited by
          #4

          Note though, that there is a difference between
          @
          const char * NOME = "xxx";
          @
          and
          @
          const char * const NOME = "xxx";
          @
          The first is a pointer with a constant value (i.e the value at the address can't change, but you can change the pointer to point to a different value at another address), the second a constant pointer to constant content (i.e. the pointed to address and the content at that address don't change). The later one is probably what you want.

          1 Reply Last reply
          0
          • S Offline
            S Offline
            spode
            wrote on 7 Aug 2011, 11:30 last edited by
            #5

            ah, so for const QString NOME = "xx"; i must write const char * NOEM = "xx"; ? thank you! =)

            1 Reply Last reply
            0
            • L Offline
              L Offline
              loladiro
              wrote on 7 Aug 2011, 11:44 last edited by
              #6

              @const char * const NOEM@
              if you want it to be a true constant

              1 Reply Last reply
              0
              • G Offline
                G Offline
                goetz
                wrote on 7 Aug 2011, 11:47 last edited by
                #7

                [quote author="spode" date="1312716608"]ah, so for const QString NOME = "xx"; i must write const char * NOEM = "xx"; ? thank you! =)[/quote]

                Why don't you just write:

                @
                const QString NOME = QString("xxx");
                @

                Why deal with that ancient char arrays?

                http://www.catb.org/~esr/faqs/smart-questions.html

                1 Reply Last reply
                0
                • L Offline
                  L Offline
                  loladiro
                  wrote on 7 Aug 2011, 12:04 last edited by
                  #8

                  It's less overhead overhead when copying and comparing (since the pointer itself is const). Also even with QString you still have "xxx" as a const char* and then you create a unicode representation of it in memory (which is additional overhead). It depends on how often you actually need to display that to the user. If you need it more as an internal constant const char * const is perfectly fine IMO.

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    goetz
                    wrote on 7 Aug 2011, 14:46 last edited by
                    #9

                    And you're lost with anything else than 7 bit ASCII; as soon as other QStrings are involved it's probably converted again and again.

                    Of course there are use cases for plain const char * constants, but IMO they are very limited.

                    It's for a reason that ever serious C++ framework has its string classes :-)

                    http://www.catb.org/~esr/faqs/smart-questions.html

                    1 Reply Last reply
                    0

                    2/9

                    7 Aug 2011, 11:01

                    topic:navigator.unread, 7
                    • Login

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