Is this legal?

Started by
1 comment, last by MENTAL 20 years, 7 months ago
1) Can i use "using namespace xxx" when writing function bodies for "namespace xxx", for example:

namespace XXX
{
    void SomeFunc ();
};

using namespace XXX;

void SomeFunc ()
{
    printf ("Hello!");
}

2) If i have some nested namespaces defined in multiple files, do i have to include the file containing the namespace definition to use the namespace?

// In Foo.h

namespace BigSpace
{
    namespace Foo
    {
        void FooFunc ();
    }
}

// In Bar.h

namespace BigSpace
{
    namespace Bar
    {
        void BarFunc ();
    }
}

// In Bar.cpp

#include "Bar.h"

void BigSpace::Bar::BarFunc ()
{
    BigSpace::Foo:FooFunc ();
}

would that run, or do i have to include "Foo.h". I''m hopeing so, because that way I can keep parts of the namespace hidden from other code modules unless they specifically need them. thanks
Advertisement
quote:Original post by MENTAL
1) Can i use "using namespace xxx" when writing function bodies for "namespace xxx", for example:

No.

Try this:
namespace XXX{    void SomeFunc();}namespace XXX{    void SomeFunc() { ... }}// ORvoid XXX::SomeFunc() { ... }


quote:2)


Of course you Have to include Foo.h. Before Foo.h is included in Bar.cpp, the compiler doesn''t even know that BigSpace::Foo::FooFunc() exists; it''s never heard of it.


How appropriate. You fight like a cow.
thanks for the reply. i thought that might be the case with 2, but the whole "namespaces don''t need to be defined in a single file" bit threw me a bit .

This topic is closed to new replies.

Advertisement