Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Namespace
(section)
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.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)