관리 메뉴

도드넷

네임스페이스(이름공간) using namespace 란 무엇인가? 본문

창고/C++ [폐쇄]

네임스페이스(이름공간) using namespace 란 무엇인가?

도드! 2014. 9. 4. 11:02
반응형




C++에서 네임스페이스(이름공간) using namespace 란 무엇인가?


namespace

In general, a namespace is a container for a set of identifiers (also known as symbols, names). Namespaces provide a level of direction to specific identifiers, thus making it possible to distinguish between identifiers with the same exact name.


네임스페이스

보통, 네임스페이스는 아이덴티파이어(상징, 이름)를 담은것이다.

네임스페이스는 레벨로 구분하여 같은 이름의 식별자를 사용하게 해준다.  


- 출처 : 구글-위키



Consider a situation, when we have two persons with the same name, Zara, in the same class. Whenever we need to differentiate them definitely we would have to use some additional information along with their name, like either the area if they live in different area or their mother or father name, etc.


Same situation can arise in your C++ applications. For example, you might be writing some code that has a function called xyz() and there is another library available which is also having same function xyz(). Now the compiler has no way of knowing which version of xyz() function you are referring to within your code.


A namespace is designed to overcome this difficulty and is used as additional information to differentiate similar functions, classes, variables etc. with the same name available in different libraries. Using namespace, you can define the context in which names are defined. In essence, a namespace defines a scope.


이런 경우를 생각해보라, 2명의 사람이 있는데 두명 다 이름이 "자라"다. 우린 이 둘을 구분해야해서 이들의 이름에 추가 정보를 달아준다. 음... 예를들어 그들이 사는 지역이름, 엄마아빠 이름 이라던지 등등.

(자라_산티아고, 자라_뉴욕)


같은 상황이 C++에서도 일어난다. 예를들어 코딩을 하는데 같은 이름의 xyz() 함수가 2개있다고 하자 컴파일러는 어느 버전의 xyz함수를 사용해야할지 모른다. 


네임스페이스는 이런 상황을 극복하기 위해 디자인됬다. 비슷한 함수, 클래스, 변수를 구분짓기 위해서.

네임스페이스의 본질은 영역을 구분하는데에 있다. 


- 출처 : http://www.tutorialspoint.com/cplusplus/cpp_namespaces.htm



 네임스페이스(이름공간) 사용예 



네임스페이스(이름공간) 사용법 : 

정의(define) - namespace 네임스페이스 { }

소환(call) - 네임스페이스_이름::함수/변수;  

기본사용(default) - using namespace 네임스페이스_이름;

* 기본사용을 선언하면 해당 네임스페이스의 요소들(함수/변수)을 쓸때마다 앞에 네임스페이스_이름을 안붙여도 됨.

 


반응형
Comments