Advertisement

External linkagewith constants...

Started by October 04, 2002 09:18 PM
0 comments, last by Arek the Absolute 21 years, 11 months ago
I''ve found ways of dodging the issue, but it really seems wrong to me that I can''t find a way to do this. Is there a way I can have a constant be used in another source file other than the one it originated in? To quote the C++ Black Book (the quickest book I had on hand): "External constant variables are external variables declared with the const keywrod and are available to all code in the file, but they are restricted to the file they''re defined in and have global scope, internal linkage." How can this be? I mean, if that''s the case I don''t understand how they''re external at all! I mean, I can only use them in the file they''re defined in, so I don''t understand how these "external constant variables" are different from a normal constant. And more importantly, I don''t see any way of actually using constants defined in one source file in another source file. What''s going on here...? -Arek the Absolute
-Arek the Absolute"The full quartet is pirates, ninjas, zombies, and robots. Create a game which involves all four, and you risk being blinded by the sheer level of coolness involved." - Superpig
You can put constants in headers without any problems. It''s variables that need to be declared extern. Just note that you can''t control the order of initialisation of global variable (i.e. you can''t control whether bar or quux will be initialised first).

** foo.h
const int foo = 5;
extern int bar;

** foo.cc
int bar = 22;

** bar.cc

#include "foo.h"

int quux = bar; // don''t !

int main()
{
int a = foo;
int b = bar;
};

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement