How do I use namespaces?
Namespace allow to code dierent entities without bothering about unicity of names.
A namespace is a logical entity of code. A namespace can be included into another
namespace (and so on). If a namespace is anonymous, its code is only accessible from
the compilation unit (.cpp le), thus equivalent to using static and extern in C.
// in file1.cpp
namespace name1
{
void function1() { /* ... */ }
namespace name2
{
void function2() { /* ... */ }
}
}
namespace // anonymous namespace
{
void function() { /* ... */ }
}
void function3() { function(); } // ok
// in file2.cpp
void function4a() { function1(); } // error
void function4c() { name1::function2(); } // error
void function4c() { function(); } // error
void function4b() { name1::function1(); } // ok
void function4d() { name1::name2::function2(); } // ok
using namespace name1; //makes accessible the entire namespace name1
void function4e() { function1(); } // ok
void function4f() { name2::function1(); } // ok
using name1::name2::function2; //makes accessible function2 only
void function3g() { function2(); } // ok
0 comments:
Post a Comment