Connecting Tech Pros Worldwide Help | Site Map

Nested namespaces

maverik
Guest
 
Posts: n/a
#1: Nov 5 '08
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.
Victor Bazarov
Guest
 
Posts: n/a
#2: Nov 5 '08

re: Nested namespaces


maverik wrote:
Quote:
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
Closed Thread