1

We have in code one common namespace MainNamespace and a lot of namespace per module eg. ModuleNamespace, DifferentModuleNamespace. Module namespaces are inside the main namespace.

When we create a new class and need another class from different module we have to declare some using to avoid writing full namespace path.

What is consider as a good practice in such situation:

Using namespace with full path:

namespace MainNamespace {
namespace ModuleNamespace {

using MainNamespace::DifferentModuleNamespace::Foo;

       class MyClass {
           void method(Foo);
       };
}
}

or remove MainNamespace namespace from path:

namespace MainNamespace {
namespace ModuleNamespace {

using DifferentModuleNamespace::Foo;

   class MyClass {
       void method(Foo);
   };
}
}

or maybe different approach is better?

Edit:

Ok, maybe different question. Is there any situation when using absolute paths to namespace (using A = Main::ModuleA::A;) will be safer than using short paths(using A = ModuleA::C;). When we do it in the same main namespace.

file A.h:

namespace Main 
{
    namespace ModuleA {
        class A
        {
        public:
            A();
            ~A();
        };

        class C
        {
        public:
            C();
            ~C();
        };
    }
}

file B.h:

  #include "A.h"

    namespace Main {
        namespace ModuleB {

            class B
            {
                using A = Main::ModuleA::A;
                using A = ModuleA::C;

            public:
                B();
                ~B();
                void foo(A a);
                void bar(C c);
            };
        }
    }
1

2 Answers 2

2

The better approach would be to declare class-level type alias for Foo and to remove using declaration from namespace scope. This will help to prevent name collisions when other classes from ModuleNamespace decide to use Foo from somewhere else.

class MyClass {
   using Foo = DifferentModuleNamespace::Foo;
   void method(Foo);
};

Alternatively, if Foo is supposed to be used in various other places of inside of ModuleNamespace then it would be worth to make a namespace-scope type alias (potentially residing in a separate header file):

// MainNamespace/ModuleNamespace/Foo.hpp
namespace MainNamespace {
namespace ModuleNamespace {

using Foo = DifferentModuleNamespace::Foo;

}
}

#include <MainNamespace/ModuleNamespace/Foo.hpp>

namespace MainNamespace {
namespace ModuleNamespace {

class MyClass {
     void method(Foo);
};

}
}
Sign up to request clarification or add additional context in comments.

1 Comment

Declare class-level type alias probably fit to my case. Anyway I'm still confuse. Should I use: DifferentModuleNamespace::Foo or MainNamespace::DifferentModuleNamespace::Foo?
0

1) Avoid declaring 'using namespace' to avoid conflict ( eg init() here will conflict ). Although class member functions will not suffer from name conflict, however public helper functions could. Similar named helper functions eases in loading inherited objects together, such as in factory design pattern, and namespace will become mandatory in such case.

2) Avoid using suffix namespace as it is implied.

3) Use namespace for clarity and to avoid conflict ( eg core::module::create_factory() makes it lucid)

namespace core{
namespace module1 {

       class MyClass1 {
           void method(Foo)
              {
                 module2::init();
              }
       };
   //HelperFunction
     void init();

}
}


namespace core{
namespace module2{

   class MyClass2 {
          void method(Foo)
              {
                 module1::init();
              }
   };
     //HelperFunction
     void init();
}
}

3 Comments

Isn't better to remove core:: from core::module2::init() and core::module1::init(). Both are already in core namespace.
In MyClass2 you can use module1::init(); and it is correct or I understand something wrong?
@developer: edited, yes you can use module1::init() directly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.