[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
CppNamespace
The most common namespace to be in, in namespace std. You can set the namespace using 'using namespace' or call the namespace and use the scope operator ('::').
Avoid using namespace std, as this will pollute the global namespace [5,6]. Use namespaces to express logical structure [1]. Place every non-local name, except main(), in some namespace [2]. Design a namespace so that you can conveniently use it without accidentally gaining access to unrelated namespaces [3]. Avoid very short names for namespaces [4].
namespace is not supported in C.
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
CppNamespace
(C++) Namespace
Keyword that enables you to give functions different namespaces. This enables that multiple functions can have the same name, but different namespaces, without unexpected behaviour.The most common namespace to be in, in namespace std. You can set the namespace using 'using namespace' or call the namespace and use the scope operator ('::').
Avoid using namespace std, as this will pollute the global namespace [5,6]. Use namespaces to express logical structure [1]. Place every non-local name, except main(), in some namespace [2]. Design a namespace so that you can conveniently use it without accidentally gaining access to unrelated namespaces [3]. Avoid very short names for namespaces [4].
Example of namespace
In this example, the function sayHello in defined in two different namespace and individually called.namespace myNamespaceA { void sayHello() { std::cout << "Hello from myNamespaceA !" << std::endl; } } namespace myNamespaceB { void sayHello() { std::cout << "Hello from myNamespaceB !" << std::endl; } } int main() { myNamespaceA::sayHello(); myNamespaceB::sayHello(); }
- include <iostream>
Hello world program using 'using namespace'
using namespace std; int main(int argc, char* argv[]) { cout <<"Hello World" << endl; return 0; }
- include <iostream>
Hello world program using scope operator
int main() { std::cout <<"Hello World" << std::endl; }
- include <iostream>
namespace is not supported in C.
'namespace' links
Code links
Reference
- 1) Bjarne Stroustrup. The C++ Programming Language (3rd edition).ISBN: 0-201-88954-4. Item 8.5.1: 'Use namespaces to express logical structure'
- 2) Bjarne Stroustrup. The C++ Programming Language (3rd edition).ISBN: 0-201-88954-4. Item 8.5.2: 'Place every non-local name, except main(), in some namespace'
- 3) Bjarne Stroustrup. The C++ Programming Language (3rd edition).ISBN: 0-201-88954-4. Item 8.5.3: 'Design a namespace so that you can conveniently use it without accidentally gaining access to unrelated namespaces'.
- 4) Bjarne Stroustrup. The C++ Programming Language (3rd edition).ISBN: 0-201-88954-4. Item 8.5.4: 'Avoid very short names for namespaces'
- 5) Bjarne Stroustrup. The C++ Programming Language (3rd edition). 1997. ISBN: 0-201-88954-4. Chapter C.14.15: 'Don't pollute the global namespace'
- 6) C++ FAQ Lite: http://www.parashift.com/c++-faq-lite/coding-standards.html#faq-27.5 :
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
