C++ global const question
-
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!
-
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!
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. -
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!
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"
-
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. -
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!
-
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!
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. -
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.
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#define
s as they are type-checked and that is solid ground for morally sound C++ coding. ~Nitron.
ññòòïðïðB A
start