[C++] namespace
Updated:
개요
- 이름 충돌을 방지하는 방법
- 이름 없는 네임스페이스의 경우 static을 사용한 것처럼 해당 파일에서만 접근 가능
코드
#include <iostream>
using namespace std;
namespace {
void foo() { cout << "::foo call" << endl; }
} // namespace
namespace ns_1 {
void foo() { cout << "ns_1::foo call" << endl; }
} // namespace ns_1
namespace ns_2 {
void foo() { cout << "ns_2::foo call" << endl; }
} // namespace ns_2
int main() {
foo();
ns_1::foo();
ns_2::foo();
return 0;
}
실행 결과
::foo call
ns_1::foo call
ns_2::foo call