2
namespace Stuff
{
    class Sprite
    {
    };

    class Circle : Stuff::Sprite
    {
    };
}

Will this work, or will this look for Stuff::Stuff::Sprite?

edit: forgot some semicolons.

edit 2: I'm accepting @Omnifarious' answer because once he edited it with @Vlad's help, it was the most complete answer. thanks also to @Vlad.

  • 1
    It will actually lookup for Stuff::Sprite. – cpx Dec 23 '10 at 00:55
  • You suppose a false dichotomy here. It looks for `::Stuff::Stuff` first (which it doesn't find), then for `::Stuff` (which it does find). After it finds a matching namespace name, it then looks in that namespace for the type name (in this case `Sprite`). – Omnifarious Dec 23 '10 at 01:01

2 Answers2

6

This will work unless there is a namespace Stuff::Stuff visible at definition point.

Example:

namespace Stuff
{
    namespace Stuff
    {
    }

    class Sprite
    {
    };

    class Circle : public Stuff::Sprite // compile error,
    {                                   // looks for Stuff::Stuff::Sprite
    };
}

Without the inner namespace Stuff it would work.

Vlad
  • 35,022
  • 6
  • 77
  • 199
5

It will work if you put in the ; after the class definitions. But it will not work in quite the way you expect. First, it tries to figure out what namespace you're talking about. First it will looks for ::Stuff::Stuff, and when it doesn't find it, it then looks for the namespace ::Stuff. It finds that namespace, so it then looks in that namespace for Sprite and finds your class.

If you have an unanchored namespace, it looks for that namespace path in the current namespace, then in the enclosing namespace, then in the enclosing enclosing namespace... etc... until it gets to the top level namespace.

See this question of mine:

In my opinion, people ought to be a lot more careful than they are about referring to namespaces. Hardly anybody ever uses a root anchored namespace specification, even when they should be because they really do mean a specific absolute namespace and not a name relative to the current namespace.

Here are a couple of interesting cases:

 1 namespace Bar {
 2
 3 class A {
 4 };
 5
 6 } // end namespace ::Bar
 7
 8 namespace Foo {
 9
10 class A {
11 };
12
13 Foo::A joe; // Refers to the A declared on line 10
14
15 namespace Foo {
16 }
17
18 Foo::A fred; // Error, finds ::Foo::Foo and doesn't find A there.
19 ::Foo::A fred; // Works and refers to the A declared on line 10
20
21 Bar::A barney; // Works, and refers to the A declared on line 3
22
23 namespace Bar {
24
25 class A {
26 };
27
28 } // end namespace ::Foo::Bar
29
30 Bar::A wilma; // Works, and refers to the A declared on line 25
31 ::Bar::A betty; // Also works, and refers to the A declared on line 3
32 ::Foo::Bar::A dino; // Also works, and refers to the A declared on line 25
33 } // end namespace ::Foo
Community
  • 1
  • 1
Omnifarious
  • 54,333
  • 19
  • 131
  • 194
  • @Omnifarious: not completely so. It will look for the _namespace_ `::Stuff::Stuff`, and if the namespce is found, it will resolve the name there. Only if there's no namespace `::Stuff::Stuff`, it will try to resolve the name in `::Stuff`. See an example in my answer. – Vlad Dec 23 '10 at 00:52
  • @Vlad - Interesting. I will run a test to see if that's really what happens. :-) – Omnifarious Dec 23 '10 at 00:53
  • @Vlad - You are correct. I will modify my answer. And I wrote a nearly equivalent example for myself. :-) – Omnifarious Dec 23 '10 at 00:55
  • @Omnifarious this would be more readable if it was indented... thanks. –  Dec 23 '10 at 01:16
  • @Jay - It is. I can indent it if you like, though I find that in general indenting the insides of namespaces isn't very useful because they are so very long. It's better to just declare very few of them in a file. I have commented which namespaces are being ended with which `}` characters, so maybe that will help. That's what I do as a standard coding practice. – Omnifarious Dec 23 '10 at 01:19
  • @Omnifarious hmm, I get what your saying. and with the comments now it is pretty readable. –  Dec 23 '10 at 01:21
  • @Omnifarious oh and as far line 30, I posted that comment before you had put it inside the Foo namespace. –  Dec 23 '10 at 01:22
  • @Jay - It always was inside the `Foo` namespace. It's just that I had forgotten the last closing brace (meaning that `::Foo` never ended within the code block I posted here) and the lack of comments made things confusing. – Omnifarious Dec 23 '10 at 01:24
  • @Omnifarious okay, so that is why it confused me. –  Dec 23 '10 at 01:40