Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Namespace
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=====C++===== In [[C++]], a namespace is defined with a namespace block.<ref>{{cite web|url=http://www.cplusplus.com/doc/tutorial/namespaces/ |title=Namespaces allow to group entities like classes, objects and functions under a name. |publisher=Cplusplus.com |access-date=2011-07-26}}</ref> <syntaxhighlight lang="cpp"> namespace abc { int bar; } </syntaxhighlight> Within this block, identifiers can be used exactly as they are declared. Outside of this block, the namespace specifier must be prefixed. For example, outside of <code>namespace abc</code>, <code>bar</code> must be written <code>abc::bar</code> to be accessed. C++ includes another construct that makes this verbosity unnecessary. By adding the line <syntaxhighlight lang="cpp"> using namespace abc; </syntaxhighlight> to a piece of code, the prefix <code>abc::</code> is no longer needed. Identifiers that are not explicitly declared within a namespace are considered to be in the global namespace. <syntaxhighlight lang="cpp"> int foo; </syntaxhighlight> These identifiers can be used exactly as they are declared, or, since the global namespace is unnamed, the namespace specifier <code>::</code> can be prefixed. For example, <code>foo</code> can also be written <code>::foo</code>. Namespace resolution in C++ is hierarchical. This means that within the hypothetical namespace <code>food::soup</code>, the identifier <code>chicken</code> refers to <code>food::soup::chicken</code>. If <code>food::soup::chicken</code> doesn't exist, it then refers to <code>food::chicken</code>. If neither <code>food::soup::chicken</code> nor <code>food::chicken</code> exist, <code>chicken</code> refers to <code>::chicken</code>, an identifier in the global namespace. Namespaces in C++ are most often used to avoid [[naming collision]]s. Although namespaces are used extensively in recent C++ code, most older code does not use this facility because it did not exist in early versions of the language. For example, the entire [[C++ Standard Library]] is defined within <code>namespace std</code>, but before standardization many components were originally in the global namespace. A programmer can insert the <code>using</code> directive to bypass namespace resolution requirements and obtain backwards compatibility with older code that expects all identifiers to be in the global namespace. However the use of the <code>using</code> directive for reasons other than backwards compatibility (e.g., convenience) is considered to be against good code practices.
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Namespace
(section)
Add topic