nakka soft world !
namespace 본문
namespace
1. 선언
namespace Audio
{
void init()
{
cout << "Audio Init" << endl;
}
}
namespace Video
{
void init()
{
cout << "Video Init" << endl;
}
}
2. 사용
#include <iostream>
int main()
{
std::cout << "hello" << std::endl; // 전체이름 사용한 방식
using std::cout; // using 선언(declaration)
cout << "hello" << std::endl;
using std::endl;
cout << "hello" << endl;
}
#include <iostream>
using namespace std;
int main()
{
cout << "hello" << endl;
}
#include <iostream>
using std::cout;
int main()
{
cout << "hello" << std::endl;
}
3. 인자기반 탐색(Argument Dependant Lookup, ADL)
namespace Audio
{
struct Card
{
};
void init(){}
void useCard(Card c) {}
}
int main()
{
Audio::Card c; // OK
//init(); // Error
useCard(c) // OK. 컴파일러가 일단 전역에 useCard 함수가 있는지 찾고 없으면 namespace에서 찾음. 이때 있기에 컴파일에 문제 없음. 이를 ADL이라고 함
}
4. 이름 없는 이름 공간(unamed namspace)
namespace
{
void foo() {}
}
// C에서의 static void foo() {} 와 동일함
int main()
{
foo(); // 동일 파일에 있으면 OK, 다른 파일에 있으면 Compiler Error
}
'프로그래밍언어 > C++' 카테고리의 다른 글
C++ Explicit Casting (0) | 2017.03.15 |
---|---|
reference (0) | 2017.03.15 |
동적 메모리 할당 (0) | 2017.03.14 |
delete function, suffix return type, trailing return (0) | 2017.03.14 |
function template (0) | 2017.03.14 |