Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ using namespace statement

Tags:

c++

namespaces

namespace MyNamespace
{
    static void foo1()
    {

    }
}

using namespace MyNamespace;

class MyClass
{
    void foo2()
    {
        ::foo1();
    }    
};

The scope resolution operation :: means using method in the global namespace. Here we can use ::foo1(). This means method foo1() is in the global namespace, am I right?

My question is, does using namespace ANAMESPACE_NAME mean we import all elements form the namespace ANAMESPACE_NAME into the global namespace?

like image 982
softempire Avatar asked Oct 09 '11 06:10

softempire


1 Answers

No. "using namespace ANAMESPACE_NAME" mean we import all elements into current scope.

You can write something like this:

namespace A {
    int i = 10, j = 20;
}

int f()
{
    using namespace A;  // injects names from A into the global scope.
    return i * j;       // uses i and j from namespace A.
}

int k = i * j; // Error: undefined variant i and j.
like image 98
ytj Avatar answered Sep 30 '22 20:09

ytj