[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Nested namespaces

maverik

11/5/2008 3:19:00 PM

Hi all.

I have several nested namespaces:

A::B::C::MyClass;

Can I write in MyClass.h

namespace A::B::C {
class MyClass {
...
}
}

instead

namespace A {
namespace B {
namespace C {
class MyClass {
...
}
}
}
}

?

When I try the first method, VC8.0 blames me:
Error 1 error C2653: 'A' : is not a class or namespace name in
MyClass.h

Thanks.
1 Answer

Victor Bazarov

11/5/2008 3:46:00 PM

0

maverik wrote:
> Hi all.
>
> I have several nested namespaces:
>
> A::B::C::MyClass;
>
> Can I write in MyClass.h
>
> namespace A::B::C {
> class MyClass {
> ...
> }
> }
>
> instead
>
> namespace A {
> namespace B {
> namespace C {
> class MyClass {
> ...
> }
> }
> }
> }
>
> ?
>
> When I try the first method, VC8.0 blames me:
> Error 1 error C2653: 'A' : is not a class or namespace name in
> MyClass.h

Apparently not. What you can do, however, is to define namespace
aliases for those frequently used nested namespaces:

Somewhere you first define

namespace A { namespace B { namespace C {} } }

Then you can do

namespace ABC = A::B::C;

The problem, however, is that you cannot reopen the namespace for adding
some declarations to it using its namespace alias. For instance, this
works:

namespace A { namespace B { namespace C { class D; } } }
namespace ABC = A::B::C;

class ABC::D {
D();
};

int main () {
ABC::D d; // error - private constructor

return 0;
}

But this won't:

namespace A { namespace B { namespace C { } } }
namespace ABC = A::B::C;

namespace ABC { // *** reopen to add class D declaration/definition
class D {
D();
};
}

int main () {
ABC::D d; // error - private constructor
return 0;
}

It will flag the syntax error on line ***. I am not sure if this has
been identified as a defect in the Standard, though.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask