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++data-structures
2 Posts 2 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 Let's say I defined a global const in a header file (which will be #included by many files) as follows: const int MAX_CLIENT = 1000; My question is, will the compiler replace each occurence of MAX_CLIENT with 1000? Should I declare MAX_CLIENT as static const instead? And will the same rule apply if MAX_CLIENT is not an int, but a char array? Thanks!

    R 1 Reply Last reply
    0
    • I Indrawati

      Hi Let's say I defined a global const in a header file (which will be #included by many files) as follows: const int MAX_CLIENT = 1000; My question is, will the compiler replace each occurence of MAX_CLIENT with 1000? Should I declare MAX_CLIENT as static const instead? And will the same rule apply if MAX_CLIENT is not an int, but a char array? Thanks!

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

      Indrawati wrote: will the compiler replace each occurence of MAX_CLIENT with 1000? Yes and no :). It's not a direct textual substitution like a #define, but when the compiler is optimising code it will substitute the variable reference by its value. Indrawati wrote: Should I declare MAX_CLIENT as static const instead? If your header file is used by multiple source files, then yes, otherwise you'll get a bunch of multiple initialisation errors from the linker. Indrawati wrote: And will the same rule apply if MAX_CLIENT is not an int, but a char array? Yes. If it's a char* though, such as char *message = "Message", then make it fully constant, ie. const char * const message = "Message", otherwise the compiler might not treat it as a true constant.

      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
      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