Re: Heap stack Class questons
On Sun, 6 Jun 2010 08:09:15 -0400, "RB" <NoMail@NoSpam> wrote:
Thanks Doug for further explanation:
4 quick questions:
If I use
namespace nSpace1{ int nInt; }
cout << nSpace::nInt ;
Is the scope of this namespace is file, correct ?
No, the namespace has global scope, the global scope itself being an
unnamed namespace.
And to use it in another file I would have to put
using namespace nSpace; // correct ?
And then if I did have the using namespace nSpace;
(in another file or the declared file)
I could just write
cout << nInt ; // correct ?
No. The compiler has to see the declaration before you can use it. To use
nInt in multiple translation units without creating multiple objects and
causing link-time multiple definition errors, it would have to be declared
extern and defined in exactly one place, just like a global variable.
"if " I am correct so far, I would at this point surmise that
one could further put this in included header files as,
#ifndef <nSpace>
#define using namespace nSpace;
#endif
Correct ?
No. Namespace names aren't macros and thus can't be used with #if, and I
have no idea what you're trying to do with "#define using". You shouldn't
redefine keywords.
And finally I would surmise that namespace is to define a type
of scope ( Label reference? for lack of a better description )
but not lifetime or duration which is still decided by other factors.
Correct ?
Right, but it's simpler perhaps than you imply. Namespaces exist only in
the global scope or inside other namespaces, so all objects defined in a
namespace have static storage duration.
--
Doug Harrison
Visual C++ MVP