Usage and namespace of :: in c++

1.global scope (global scope)

for example
int a;

void test ()
{
  int a = ::a;//Use global variable a to assign value to local variable a
}

2.class scope (class scope)

for example
class A
{
  public:
    int test();
}

int A::test()//Indicates that test belongs to A
{
 return 0;
}

3. Namespaces

3.1 Namespace simple declaration and invocation:

namespace na

{

   int a;

   char c;

}

call na::a

3.2 Nested use

namespace ns1

{

                 int a;

                 char c = 10;

                 void fun();

                 int Add(int a, int b)

                {

                                 return a + b;

                }

                 namespace ns2

                {

                                 int b;

                }

}

If you want to call b then: ns1::ns2:b

3.3: Aliasing namespaces

namespace Television

{...}

namespace Tv=Television;

Tv:: can call members in Television

3.4: Using using xxx::xxx

 using ns1::a; If this statement is used, the place where a is used does not need to add the :: symbol

3.4.2: Using using namespace xxx

using namespace ns1 If you use this statement, you can directly use the internal members of ns1 without adding the :: symbol

3.5: Nameless namespaces

namespace

{

   void fun();

}

Only use the fun function within this file

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325259683&siteId=291194637