logo

C++의 네임스페이스 | 세트 3(액세스, 헤더 생성, 중첩 및 앨리어싱)

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

자바의 문자열 메소드

네임스페이스에 액세스하는 다양한 방법: C++에는 네임스페이스 변수와 함수에 액세스하는 두 가지 방법이 있습니다.



네임스페이스 정의:

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

namespace  namespace_name   
{
// code declarations i.e. variable  (int a;)
method (void add();)
classes ( class student{};)
}

닫는 중괄호 뒤에는 세미콜론(;)이 없다는 점에 유의하세요.
함수 또는 변수의 네임스페이스 지원 버전을 호출하려면 다음과 같이 네임스페이스 이름을 앞에 추가하세요.
네임스페이스_이름: :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 지시문 지점부터 지시문이 있는 범위 끝까지 표시됩니다. 외부 범위에 정의된 동일한 이름을 가진 엔터티는 숨겨집니다.



중첩된 네임스페이스:

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

namespace namespace_name1   
{
  // code declarations
  namespace namespace_name2 
  {
     // code declarations
  }
}

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


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

YouTube 비디오 VLC를 다운로드하는 방법
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; } 

1. 일반적인 방법  

CPP
// C++ program to demonstrate accessing of variables // in normal way i.e. using '::' #include    using namespace std; namespace geek {  int rel = 300;  } int main() {  // variable ‘rel’ accessed   // using scope resolution operator  cout &lt;&lt; geek::rel &lt;&lt; &quot;n&quot;; // prints 300  return 0; } 

출력 :

300  

2. '사용' 지시문  

CPP
// C++ program to demonstrate accessing of variables // in normal way i.e. using 'using' directive #include    using namespace std; namespace geek {  int rel = 300;  } // use of ‘using’ directive using namespace geek; int main() {  // variable ‘rel’ accessed   // without using scope resolution variable  cout &lt;&lt; rel &lt;&lt; &quot;n&quot;; //prints 300    return 0; } 

산출:

자바 스위치 케이스
300  

헤더 파일에서 네임스페이스 사용 한 파일에 네임스페이스를 만들고 다른 프로그램을 사용하여 콘텐츠에 액세스할 수 있습니다. 이는 다음과 같은 방식으로 수행됩니다.

  • 두 개의 파일을 만들어야 합니다. 나중에 사용하려는 네임스페이스와 모든 데이터 멤버 및 멤버 함수를 포함하는 것입니다.
  • 그리고 다른 프로그램은 첫 번째 프로그램을 직접 호출하여 그 안에 있는 모든 데이터 멤버와 멤버 함수를 사용할 수 있습니다.

파일 1  

CPP
// file1.h  namespace foo {  int value()   {   return 5;   } } 

파일 2  

CPP
// file2.cpp - Not to be executed online #include    #include file1.h // Including file1 using namespace std; int main ()  {  cout &lt;&lt; foo::value();  return 0; } 

여기서는 네임스페이스가 file1.h에 생성되고 해당 네임스페이스의 value()가 file2.cpp에서 호출되는 것을 볼 수 있습니다. 중첩된 네임스페이스 C++에서는 네임스페이스가 중첩될 수도 있습니다. 즉, 하나의 네임스페이스가 다른 네임스페이스 안에 포함될 수도 있습니다. 네임스페이스 변수의 확인은 계층적입니다. 

CPP
// C++ program to demonstrate nesting of namespaces #include    using namespace std; // Nested namespace namespace out {  int val = 5;   namespace in  {  int val2 = val;   } } // Driver code int main() {  cout &lt;&lt; out::in::val2; // prints 5  return 0; } 

출력 :

5  

네임스페이스 앨리어싱: C++에서는 사용 편의성을 위해 네임스페이스 이름에 별칭 이름을 사용할 수 있습니다. 기존 네임스페이스는 다음 구문을 사용하여 새 이름으로 별칭을 지정할 수 있습니다.

사자와 호랑이의 차이
namespace new_name = current_name;  
CPP
#include    namespace name1  {  namespace name2   {  namespace name3   {  int var = 42;  }  } } // Aliasing  namespace alias = name1::name2::name3;   int main() {  std::cout &lt;&lt; alias::var &lt;&lt; 'n'; } 

출력 :

42  

GeeksforGeeks를 좋아하고 기여하고 싶다면 다음을 사용하여 기사를 작성할 수도 있습니다. write.geeksforgeeks.org 또는 기사를 [email protected]로 우편으로 보내세요. GeeksforGeeks 메인 페이지에 나타나는 기사를 보고 다른 Geeks를 도와주세요.