Home > Blockchain >  Multiple Namespaces in a single line in C
Multiple Namespaces in a single line in C

Time:09-24

I have a doubt to ask.

In C , What do multiple namespaces in a single line refer to?

For example:

#include<iostream>
#include<conio>

using namespace university::project::work ; 

Any help will be appreciated!

Thanks in advance!

CodePudding user response:

Namespaces in C can be nested, thus the multiple namespaces you refer to.

Example:

namespace foo {
    std::string cool_phrase;
    namespace bar {
        void func(int n) { ... }
    }
}

/* We will refer to cool_phrase as such: */
std::cout << foo::cool_phrase << std::endl;

/* We will call func as such: */
foo::bar::func(10);

CodePudding user response:

For example if you have the following namespaces.

namespace A
{
    namespace B
    {
        namespace C
        {
            int d;
        }               
    }
}

When you write using namespace A::B::C; you have a direct access to d. If you have written using namespace A; you had to access d by B::C::d.

#include <iostream>
namespace A
{
    namespace B
    {
        namespace C
        {
            int d;
        }               
    }
}
int main()
{
    using namespace A;
    B::C::d = 0; //OK
    C::d = 0; //NOT OK, C is not visible from A
    using namespace A::B;
    C::d = 0; //OK
    d = 0; //NOT OK
    using namespace A::B::C;
    d = 0; // OK
}
  • Related