Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Namespace and class with the same name?

I'm organizing a library project and I have a central manager class named Scenegraph and a whole bunch of other classes that live in the Scenegraph namespace.

What I'd really like is for the scenegraph to be MyLib.Scenegraph and the other classes to be MyLib.Scenegraph.*, but it seems the only way to do that would be to make all the other classes inner classes of Scenegraph in the Scenegraph.cs file and that's just too unwieldy.

Instead, I've organized it as Mylib.Scenegraph.Scenegraph and MyLib.Scenegraph.*, which sort of works but I find Visual Studio gets confused under some conditions as to whether I am referring to the class or the namespace.

Is there a good way to organize this package so it's convenient for users without glomming all my code together in an unmaintainable mess?

like image 521
user430788 Avatar asked Sep 27 '22 22:09

user430788


People also ask

Can a class and a namespace have the same name C++?

To clarify this answer: anything can have the same name as anything else if they are in different namespaces; a class cannot have the same name as a namespace if they are both in the same namespace.

Is namespace same as class?

The namespace and classes are two different concepts. Classes are datatypes. Classes are basically extended version of structures. Classes can contain data members and functions as members, but namespaces can contain variables and functions by grouping them into one.

Can two namespaces have same name?

Absolutely nothing, as far as you avoid to use the using namespace <XXX>; directive. As David Vandevoorde said, the “fully qualified name” (the standard term for him “complete name”) is different, while you have to prefix the name with, <namespace>::, and compiler can make the difference between both.

Is the namespace the same as the file name?

No, the namespace can be anything, they don't have to be the same as the filename. It is good practice to have the namespaces correspond to physical file paths (folders in your solution). So if you have a file in .


2 Answers

I don't recommend you to name a class like its namespace, see this article.

The Framework Design Guidelines say in section 3.4 “do not use the same name for a namespace and a type in that namespace”. That is:

namespace MyContainers.List 
{ 
    public class List { … } 
}

Why is this badness? Oh, let me count the ways.

You can get yourself into situations where you think you are referring to one thing but in fact are referring to something else. Suppose you end up in this unfortunate situation: you are writing Blah.DLL and importing Foo.DLL and Bar.DLL, which, unfortunately, both have a type called Foo:

// Foo.DLL: 
namespace Foo { public class Foo { } }

// Bar.DLL: 
namespace Bar { public class Foo { } }

// Blah.DLL: 
namespace Blah  
{   
using Foo;   
using Bar;   
class C { Foo foo; } 
}

The compiler gives an error. “Foo” is ambiguous between Foo.Foo and Bar.Foo. Bummer. I guess I’ll fix that by fully qualifying the name:

   class C { Foo.Foo foo; } 

This now gives the ambiguity error “Foo in Foo.Foo is ambiguous between Foo.Foo and Bar.Foo”. We still don’t know what the first Foo refers to, and until we can figure that out, we don’t even bother to try to figure out what the second one refers to.

like image 136
pinckerman Avatar answered Oct 06 '22 18:10

pinckerman


Giving the same name to the namespace and the class can confuse the compiler as others have said.

How to name it then?

If the namespace has multiple classes then find a name that defines all those classes.

If the namespace has just one class (and hence the temptation to give it the same name) name the namespace ClassNameNS. This is how Microsoft names their namespaces at least.

like image 24
GoTo Avatar answered Oct 06 '22 17:10

GoTo