| View previous topic :: View next topic |
| Author |
Message |
Coding Junkie Guest
|
Posted: Wed Jan 28, 2004 2:26 pm Post subject: Can you extern a namespace scoped variable |
|
|
I think the subject says it all. Can you define a variable in a
namespace in one translation unit and use it in another?
If you can how do you do it with visual c 7?
I'm thinking this must be possible because aren't cin and cout
externed from within the std namespace?
| Quote: | From my experiments, the compiler seems to accept it happily and will
even allow me to take the size of the object but any attempt to use |
the object results in a linker error. If I take the object out of the
namespace and make it properly global then I can extern it
hassle-free.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ulrich Eckhardt Guest
|
Posted: Wed Jan 28, 2004 8:32 pm Post subject: Re: Can you extern a namespace scoped variable |
|
|
Coding Junkie wrote:
| Quote: | I think the subject says it all. Can you define a variable in a
namespace in one translation unit and use it in another?
|
// this can in as many TUs as you want
namespace foo
{
extern int bar; // declaration
}
// this must be in exactly one TU
int foo::bar = 42; // definition
Know that this is true regardless of whether you use namespaces or not.
Uli
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ben Hutchings Guest
|
Posted: Thu Jan 29, 2004 9:41 am Post subject: Re: Can you extern a namespace scoped variable |
|
|
Coding Junkie wrote:
| Quote: | I think the subject says it all. Can you define a variable in a
namespace in one translation unit and use it in another?
If you can how do you do it with visual c 7?
|
Yes. Here's an example composed of three files:
foo.hpp:
namespace foo
{
extern int x;
}
foo.cpp:
#include "foo.hpp"
namespace foo
{
int x = 23;
}
bar.cpp:
#include "foo.hpp"
namespace bar
{
int f()
{
return foo: / 10 + foo: % 10;
}
}
| Quote: | I'm thinking this must be possible because aren't cin and cout
externed from within the std namespace?
From my experiments, the compiler seems to accept it happily and will
even allow me to take the size of the object but any attempt to use
the object results in a linker error. If I take the object out of the
namespace and make it properly global then I can extern it
hassle-free.
|
I think you are using "extern" wrongly. An object may be declared in
multiple translation units but it also needs to be defined in exactly
one. A definition doesn't use "extern".
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze@gabi-soft.fr Guest
|
Posted: Sat Jan 31, 2004 1:09 am Post subject: Re: Can you extern a namespace scoped variable |
|
|
Ben Hutchings <do-not-spam-benh (AT) bwsint (DOT) com> wrote
| Quote: | I think you are using "extern" wrongly. An object may be declared in
multiple translation units but it also needs to be defined in exactly
one. A definition doesn't use "extern".
|
In some cases, a definition doesn't need extern. But
extern int const frobitCount = 20 ;
is a definition.
--
James Kanze GABI Software mailto:kanze (AT) gabi-soft (DOT) fr
Conseils en informatique orientée objet/ http://www.gabi-soft.fr
Beratung in objektorientierter Datenverarbeitung
11 rue de Rambouillet, 78460 Chevreuse, France, +33 (0)1 30 23 45 16
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|