logo

C++의 네임스페이스 | 세트 2(확장 네임스페이스 및 이름 없는 네임스페이스)

아래 세트 1에 네임스페이스를 도입했습니다.
C++의 네임스페이스 | 세트 1(소개)

네임스페이스 정의:



네임스페이스 정의는 다음과 같이 네임스페이스 키워드로 시작하고 그 뒤에 네임스페이스 이름이 옵니다.

자바는 널이다

네임스페이스  namespace_name 
{
// 코드 선언, 즉 변수(int a;)
메소드(void add();)
수업(수업 학생{};)
}

닫는 중괄호 뒤에는 세미콜론(;)이 없다는 점에 유의하세요.
함수 또는 변수의 네임스페이스 지원 버전을 호출하려면 다음과 같이 네임스페이스 이름을 앞에 추가하세요.
네임스페이스_이름: :code;  // 코드는 변수 함수 또는 클래스일 수 있습니다.



사용 지시문:

using 네임스페이스 지시문을 사용하면 네임스페이스가 앞에 추가되는 것을 방지할 수도 있습니다. 이 지시문은 후속 코드가 지정된 네임스페이스의 이름을 사용하고 있음을 컴파일러에 알립니다. 
따라서 네임스페이스는 다음 코드에 대해 암시됩니다.

C++
#include    using namespace std; // first name space namespace first_space {  void func()  {  cout << 'Inside first_space' << endl;  } } // second name space namespace second_space {  void func()  {  cout << 'Inside second_space' << endl;  } } using namespace first_space; int main () {  // This calls function from first name space.  func();  return 0; } 

using 지시문에 도입된 이름은 일반 범위 규칙을 따릅니다. 이름은 using 지시문 지점부터 지시문이 있는 범위 끝까지 표시됩니다. 외부 범위에 정의된 동일한 이름을 가진 엔터티는 숨겨집니다.



중첩된 네임스페이스:

네임스페이스는 다음과 같이 다른 네임스페이스 내에 하나의 네임스페이스를 정의할 수 있는 중첩될 수 있습니다.

키보드에 삽입
SYNTAX: namespace namespace_name1 { // code declarations namespace namespace_name2 { // code declarations } }

다음과 같이 확인 연산자를 사용하여 중첩된 네임스페이스의 멤버에 액세스할 수 있습니다.
//namespace_name2의 멤버에 액세스합니다.
네임스페이스 네임스페이스_name1::namespace_name2 사용;
// 네임스페이스:name1의 멤버에 액세스합니다.
네임스페이스 네임스페이스_name1 사용;


위 문에서 네임스페이스_이름1을 사용하는 경우 다음과 같이 범위에서 네임스페이스_이름2의 요소를 사용할 수 있게 됩니다.

C++
#include    using namespace std; // first name space namespace first_space {  void func()  {  cout << 'Inside first_space' << endl;  }  // second name space  namespace second_space  {  void func()  {  cout << 'Inside second_space' << endl;  }  } } using namespace first_space::second_space; int main () {  // This calls function from second name space.  func();    return 0; } 


전역 공간에 둘 이상의 네임스페이스를 만드는 것도 가능합니다. 이는 두 가지 방법으로 수행할 수 있습니다. 
 

    다른 이름을 가진 네임스페이스 
     
CPP
// A C++ program to show more than one namespaces  // with different names. #include    using namespace std; // first name space namespace first {  int func() { return 5; } } // second name space namespace second {  int func() { return 10; } } int main() {  // member function of namespace  // accessed using scope resolution operator  cout &lt;&lt; first::func() &lt;&lt;&quot;n&quot;;   cout &lt;&lt; second::func() &lt;&lt;&quot;n&quot;;   return 0; } 

  • 산출: 
     
5 10
    네임스페이스 확장(같은 이름을 두 번 사용) 
    동일한 이름을 가진 두 개의 네임스페이스 블록을 생성하는 것도 가능합니다. 두 번째 네임스페이스 블록은 실제로는 첫 번째 네임스페이스의 연속일 뿐입니다. 더 간단하게 말하면 두 네임스페이스는 다르지 않지만 부분적으로 정의되는 실제로 동일하다고 말할 수 있습니다. 
     
CPP
// C++ program to demonstrate namespace extension #include    using namespace std; // first name space namespace first  {   int val1 = 500;  } // rest part of the first namespace namespace first  {   int val2 = 501;  } int main() {  cout &lt;&lt; first::val1 &lt;&lt;&quot;n&quot;;   cout &lt;&lt; first::val2 &lt;&lt;&quot;n&quot;;   return 0; } 

  • 산출: 
     
500 501


이름 없는 네임스페이스  
 

  • 동일한 프로그램에서 직접 사용할 수 있으며 고유 식별자를 선언하는 데 사용됩니다.
  • 명명되지 않은 네임스페이스에서는 네임스페이스 선언에 언급되지 않은 네임스페이스의 이름입니다.
  • 네임스페이스의 이름은 컴파일러에 의해 고유하게 생성됩니다.
  • 귀하가 생성한 이름 없는 네임스페이스는 귀하가 생성한 파일 내에서만 액세스할 수 있습니다.
  • 이름이 없는 네임스페이스는 변수의 정적 선언을 대체합니다.


 

무작위 C
CPP
// C++ program to demonstrate working of unnamed  // namespaces #include    using namespace std; // unnamed namespace declaration namespace  {  int rel = 300;  } int main() {  cout &lt;&lt; rel &lt;&lt; &quot;n&quot;; // prints 300  return 0; } 

산출: 
 

300