I have an Ah file in which there is a code:

 namespace Top { namespace Inner { namespace Bottom { class ClassA { ... } } } } 

And in another Bh file, similar code:

 namespace Top { namespace Inner { namespace Bottom { class ClassB { ... } } } } 

My question is

Why in different files to describe the same namespace names? Or during compilation or linking they somehow merge into one, and the compiler can understand that if we have the same name in file A and B , then is it the same namespace ? I have a project of more than 20 different files with the same namespace names, but with different classes inside some. In addition, if I want to connect to my project dll , in which I will have ClassC with the same hierarchy of namespaces, will it be considered the same namespace or already different? Why do I need to duplicate the names of spaces in different files?

Can this option be considered valid?

 #include "Ah" #include "Bh" void func() { Top::Inner::Bottom::ClassA(); Top::Inner::Bottom::ClassB(); } 
  • one
    The global namespace is no different from any other namespace. Your example class declaration in the custom namespace contains absolutely no differences from the everyday practice of class declaration in the global namespace. The fact that in this case some other namespace is used does not change anything. Therefore, it is not clear what exactly caused this question and what is its essence in general. - AnT

2 answers 2

Duplication of the name namespace in different files allows you to achieve ..

Briefly :

  1. Convenience navigation / orientation in the code.
  2. Addition of the existing name space with new features.

More details :

We are all human, and we are all arranged so that we can not keep many things in mind at the same time. It is much more convenient for both myself and other programmers to take out the already “ready” or less debugged code sections to a separate file ( out of sight , so to speak), so as not to keep them in my head (besides, there are rules that it is possible and you need to make to the files, but this is a separate topic). What then? Will have to share the namespace? It does not have to, because in different files you can write the same name for the namespace . Look, for example, how it is done in Qt (this code is created by default when creating a form / dialog box):

mainwindow.h

 namespace Ui { //--------^^ class MainWindow; } class MainWindow : public QMainWindow { // поля и методы }; 

aboutform.h

 namespace Ui { //--------^^ class AboutDialog; } class AboutDialog: public QDialog { // поля и методы }; 

Note that the namespace name is the same. This feature complements the existing namespace (which could have been declared somewhere before).

Among other things, using, as you said, duplication of the name, you can extend the already existing namespace without recompiling it. Nobody forbids you to do this:

 #include <iostream> namespace std { const int my_custom_value = 999; }; int main() { std::cout << "My custom std value = " << std::my_custom_value << std::endl; return 0; } 

And get on the screen:

 My custom std value = 999 

And imagine how much time it saves, because we do not need to rebuild the entire namespace.

  • less clear. thank. - raviga

It will still be the same namespace, you just add to it, the compiler understands that this is the same thing, the main thing is to correctly refer to the classes. "Why in different files to describe the same namespace name?" Virtually no difference, but whether it is advisable, it is up to you. Your option is considered valid.