• Uncategorized
  • 0

An article to understand the usage of namespace in C++

Hits: 0

understand C++in namespace

==================================

1 namespacerole

Creating names is a fundamental activity in the programming process, such as creating names for [symbolic constants] , variables, functions, structures, enumerations, classes, and objects. When a project is large, there is a greater chance that the names will conflict with each other, so there will be a series of problems when calling.

To avoid the consequences of this happening, the standard C++introduces keywords namespace( [namespace] /namespace/namespace) to control the scope of identifiers. Just as Clanguages ​​can use the statickeyword to make names visible only within this compilation unit.

2 namespaceusage

2.1 Definitionsnamespace

namespaceThe definition uses the keyword namespacefollowed by the name of the namespace, as follows:

namespace  namespace_name {
     // code declaration 
}

When calling namespacea function or variable of , you need to prepend the name of the namespace, as follows:

name::code;   // code can be a variable or a function

Precautions:

namespaceCan only be defined in the global scope (the following incorrect spelling)

void test() {
    namespace A{
        int a = 10;
    }
    namespace B{
        int a = 20;
    }
    cout << "A::a : " << A::a << endl;
    cout << "B::a : " << B::a << endl;
}

namespaceCan be nested

namespace A {
    int a = 10;
    namespace B {
        int a = 20;
    }
}

namespaceis open, you can add new members to existing ones namespaceat

namespace A {
    int a = 10;
}
namespace A {
    void func() {
        cout << "hello namespace!" << endl;
    }
}

namespaceThe declaration and implementation are separable

namespace NewSpace {
    void func1();
    void func2(int param);
}
void NewSpace::func1(){
    cout << "NewSpace::func1" << endl;
}

The definition of nameless namespacemeans namespacethat the identifier in can only be accessed within this file, which is equivalent to adding this identifier staticso that it can be used as an internal connection

namespace {
    int a = 10;
    void func() {
        cout << "hello namespace" << endl;
    }
}
void test() {
    cout << "a : " << a << endl;
    func();
}

You can namespacedefine

namespace veryLongName {
    int a = 10;
    void func() {
        cout << "hello namespace" << endl;
    }
}
void test() {
    namespace shortName = veryLongName;
    cout << "veryLongName::a : " << shortName::a << endl;
    veryLongName::func();
    shortName::func();
}

2.2 Usenamespace

Using the usingkeyword followed by the name of the namespace to be used tells the compiler that subsequent code will use the specified namespace.

// first 
namespace namespace first_space{
    void  func () {
       cout << "Inside first_space" << endl ;
   }
}
// Second 
namespace namespace second_space{
    void  func () {
       cout << "Inside second_space" << endl ;
   }
}
using namespace first_space;

void  test  ()
 {
    // call the function in the first namespace
   func();
}

You may also like...

Leave a Reply

Your email address will not be published.