C++ Primary Namespaces
C++ Namespace - Part 3
Forward: In this part of the series I talk about the different primary namespaces in C++.
By: Chrysanthus Date Published: 7 Feb 2013
Introduction
Local Namespace
The local namespace is also called the user-defined namespace. The following program shows an example:
namespace first
{
int identA = 33;
int identAA = 35;
}
//other statements
using namespace first;
int anInt = identA;
int theInt = identAA;
int main()
{
int mainInt = identA;
return 0;
}
Here, the local namespace is, first. You have seen similar things like this already. This kind of namespace is defined by you, the programmer.
The std namespace or standard namespace is defined by the C++ specification. It has modules (declarations of classes, functions and objects) that are commonly used in many programs by developers (programmers). Because it is defined by the specification (or authors of the specification), it is called the std namespace. It is written in the same way that the local namespace is written. There is no fundamental difference between a local namespace and the std namespace. However, the content of the std namespace is unique, while the content of the local namespace is of your choice.
Unlike the local namespace that is written by you and you can see the definition block in your program, the std namespace is not written by you. The standard namespace block is not written by you. It is defined by the C++ specification (authors) and coded by the C++ compiler company. You do not need to see its content while you are writing your program.
The std namespace exists in many header files. To use what is in a particular header file, you just include the header file with the corresponding #include preprocessing directive and then use the using-directive of “using namespace std;” just below the include line. Example:
#include <iostream>
using namespace std;
Any name (identifier) that is not declared in a block (or nested block) is in the global scope. Remember, a namespace is a scope. So the global namespace is the global scope.
Note: even though you have the local scope, function scope, class scope and global scope, these are not officially called namespaces. Only the namespace scope (block) and the global scope are called namespaces. The namespace scope can reoccur beyond its block with the using-directive. The other scopes cannot really be made to reoccur beyond their blocks.
Global Namespace Everywhere Below
A name declared in the global namespace, can be seen everywhere below the point of declaration. This means that the name can be seen inside blocks and nested blocks, below the point of declaration. This means that the name can be seen inside local scopes, function scopes, class scopes and local namespace scopes. That is, the name can be accessed (seen and used) in these scopes, which include the namespace scope.
Any namespace, be it local or std or global, consists fundamentally of declarations.
There is not much to say on this topic, so we take a break here and continue in the next part of the series.
Chrys
Related Courses
C++ CourseRelational Database and Sybase
Windows User Interface
Computer Programmer – A Jack of all Trade – Poem
NEXT