C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Can you extern a namespace scoped variable

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Coding Junkie
Guest





PostPosted: Wed Jan 28, 2004 2:26 pm    Post subject: Can you extern a namespace scoped variable Reply with 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?
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





PostPosted: Wed Jan 28, 2004 8:32 pm    Post subject: Re: Can you extern a namespace scoped variable Reply with quote



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





PostPosted: Thu Jan 29, 2004 9:41 am    Post subject: Re: Can you extern a namespace scoped variable Reply with quote



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:Mad / 10 + foo:Mad % 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





PostPosted: Sat Jan 31, 2004 1:09 am    Post subject: Re: Can you extern a namespace scoped variable Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.