C++ Namespaces, comparison to Java packages

后端 未结 7 866
悲&欢浪女
悲&欢浪女 2021-01-31 08:07

I\'ve done a bunch of Java coding recently and have got used to very specific package naming systems, with deep nesting e.g. com.company.project.db. This works fine

7条回答
  •  时光说笑
    2021-01-31 08:13

    In C++, the basic unit of design and implementation is the class, not the namespace. Namespaces were intended as a means of preventing name clashes in large libraries, not for expressing concepts.

    Classes have several advantages over namespaces:

    • they can have constructors and destructors
    • they can have private members
    • they cannot be re-opened

    However, I would look twice at any deeply nested relationship. That is really not a good way of designing software, and leads to unreadable code, whether you juse classes or namespaces.

提交回复
热议问题