Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. C++ global const question

C++ global const question

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++tutorial
7 Posts 6 Posters 0 Views 1 Watching
  • 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.
  • I Offline
    I Offline
    Indrawati
    wrote on last edited by
    #1

    Hi Could someone point me to some online material about C++ global consts? I need to know in what order are they initialized, where they are stored, etc. For example, in my current program, I use a string defined as: const char *const A_STRING = "abcdef"; This string is defined in a header file that is included in many other files. The code compiles and links OK, but if I changed the definition to: const char* A_STRING = "abcdef"; the linker complains that A_STRING was multiply defined. Thanks!

    J R 2 Replies Last reply
    0
    • I Indrawati

      Hi Could someone point me to some online material about C++ global consts? I need to know in what order are they initialized, where they are stored, etc. For example, in my current program, I use a string defined as: const char *const A_STRING = "abcdef"; This string is defined in a header file that is included in many other files. The code compiles and links OK, but if I changed the definition to: const char* A_STRING = "abcdef"; the linker complains that A_STRING was multiply defined. Thanks!

      J Offline
      J Offline
      Jeff Bogan
      wrote on last edited by
      #2

      You probly shouldn't use global constants and C++ in the same sentence - it is a travesty to behold. But, if you need to - stick a static keyword in front of the const. ----------------------------- All truth passes through 3 stages. First, it is ridiculed. Second, it is violently opposed. Third, it is accepted as being self-evident.

      A 1 Reply Last reply
      0
      • I Indrawati

        Hi Could someone point me to some online material about C++ global consts? I need to know in what order are they initialized, where they are stored, etc. For example, in my current program, I use a string defined as: const char *const A_STRING = "abcdef"; This string is defined in a header file that is included in many other files. The code compiles and links OK, but if I changed the definition to: const char* A_STRING = "abcdef"; the linker complains that A_STRING was multiply defined. Thanks!

        R Offline
        R Offline
        Ryan Binns
        wrote on last edited by
        #3

        Indrawati wrote: const char* A_STRING = "abcdef"; This defines a variable that is a pointer to a constant string. The pointer can be changed to point to different locations, but the value at that location can't be changed. So in effect, it's not really a constant at all. The compiler complains because you've got a multiply-initialised variable defined in multiple files. Indrawati wrote: const char *const A_STRING = "abcdef"; This truly is constant, so the compiler can safely optimise it out and prevent multiple definitions. As was said above, adding static to the front of the definition will prevent the multiple-definition problem.

        Ryan

        "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

        1 Reply Last reply
        0
        • J Jeff Bogan

          You probly shouldn't use global constants and C++ in the same sentence - it is a travesty to behold. But, if you need to - stick a static keyword in front of the const. ----------------------------- All truth passes through 3 stages. First, it is ridiculed. Second, it is violently opposed. Third, it is accepted as being self-evident.

          A Offline
          A Offline
          Anonymous
          wrote on last edited by
          #4

          Hi Thanks for the reply. I am just curious, why's global constants such a travesty in C++? I always thought they're the preferred replacements for #define preprocessor? What's the advantage that I gain by defining them as static? Thanks!

          T J 2 Replies Last reply
          0
          • A Anonymous

            Hi Thanks for the reply. I am just curious, why's global constants such a travesty in C++? I always thought they're the preferred replacements for #define preprocessor? What's the advantage that I gain by defining them as static? Thanks!

            T Offline
            T Offline
            Tim Smith
            wrote on last edited by
            #5

            Because he once read that globals where bad and doesn't really understand why. There is nothing wrong with global constants. As you noted, they are the repacement of #define. Tim Smith I'm going to patent thought. I have yet to see any prior art.

            N 1 Reply Last reply
            0
            • A Anonymous

              Hi Thanks for the reply. I am just curious, why's global constants such a travesty in C++? I always thought they're the preferred replacements for #define preprocessor? What's the advantage that I gain by defining them as static? Thanks!

              J Offline
              J Offline
              Jeff Bogan
              wrote on last edited by
              #6

              Maybe I was a bit harsh there, but under the princliple of OOP you are supposed to encapslate everything including const data. This is a purist's view, so don't take any mind if you approaching it from a practical standpoint. static will ensure that the data is instantiated in the data segment of your software, rather than on the heap or stack. That goes back to your question on when it is created. ----------------------------- All truth passes through 3 stages. First, it is ridiculed. Second, it is violently opposed. Third, it is accepted as being self-evident.

              1 Reply Last reply
              0
              • T Tim Smith

                Because he once read that globals where bad and doesn't really understand why. There is nothing wrong with global constants. As you noted, they are the repacement of #define. Tim Smith I'm going to patent thought. I have yet to see any prior art.

                N Offline
                N Offline
                Nitron
                wrote on last edited by
                #7

                Tim Smith wrote: There is nothing wrong with global constants. As you noted, they are the repacement of #define. 2nd on that! I always prefer static const globals over #defines as they are type-checked and that is solid ground for morally sound C++ coding. ~Nitron.


                ññòòïðïðB A
                start

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups