Broad Network


Explanation of the Meaning of C++ Namespace

C++ Namespace - Part 2

Forward: In this part of the series, I give you the Explanation of the Meaning of C++ Namespace.

By: Chrysanthus Date Published: 7 Feb 2013

Introduction

This is part 2 of my series, C++ Namespace. In this part of the series, I give you the Explanation of the Meaning of C++ Namespace. I begin by talking about C++ names in general. You should have read the previous part of the series before reaching here, as this is a continuation.

C++ Names
The following C++ entities can each be identified by an identifier: object, class, array, fundamental object identifier, function, enumerator, template, macro, pointer, and namespace. The identifier that identifiers the entity is also called the name of the entity.

Scopes
The main scopes are: local scope, function scope, class scope, global scope and namespace scope.

Apart from the global scope and to some extent, the namespace scope, each scope is determined by the pair of curly brackets. Local scope blocks are the if-block, loop blocks and the switch block. The scope begins from the text preceding the block and goes right through the block. This means the effectiveness (usefulness) of any name declared in the text preceding the block ends at the end of the block. Note: any name declared in the block is effective from the point of declaration to the end of the block.

The function scope is defined in a similar way to the local scope, but it deals with functions. The class scope is defined in a similar way to the local scope, but it deals with classes.

The global scope is for any name declared outside all blocks. A name declared in the global scope is effective from the point of declaration to the end of the file. Note: any name declared in the global scope can be seen inside any block below, the point of declaration. So any name declared in the global scope can be seen in any other scope including the namespace scope (see below).

The namespace scope is defined in a similar way to the local scope; however it can also exists in portions outside and below its block, with the use of the using directive and in a way, the use of the scope resolution operator (see later).

Same name of different Entity-Types in the same Scope
Can you have more than one entity of different types, but with the same name, in one scope? For example, can a fundamental object type and a function with the same name exist in one scope? No, you cannot; it cannot. Of course, you cannot have two entities of the same type with the same name in the same scope; that you should already know from the previous part of the series. Any such attempt, leads to name ambiguity or name conflict and the program does not compile. All entities in a scope must have unique names in C++, independent of whether they are of the same type or not.

Same Name in Different Scopes
You can have different entities of the same name in different scopes; the entities can be of the same type or of different types. However, if the scopes are nested, then the entity declared in the inner scope will be the one in effect in the inner scope. If there is no entity inside with the same name as the one outside, then the entity in the outer scope will be in effect outside and inside; that is OK but may not be convenient at times. The same name in different block scopes that are not nested is not a problem.

An Application
An application is a program consisting of more than one C++ files, usually written by several people; each person writing his own set of files. Such files are usually brought together using the #include preprocessing directive in a header (see later). The resulting effective file is one long file or one large file. Under such a circumstance, you can have more than one entity with the same name in the same scope, especially in the global scope. That would cause a name conflict, and the application will not compile.

It is because of the possible conflict of naming within a scope and among scopes, that the namespace was invented. The problem of same names within a scope is clear. However the one of inner and outer scope is not clear, and may or may not be seen as a problem.

Namespace as a Consolidation of Scopes
The namespace is a scope, invented to solve the problem of name conflict within a scope and among scopes. The problem solved is to give the possibilities to use the same name in different scopes and within a scope; this may not make sense to you now, but as you read on, it will be illustrated and clarified. A namespace is a scope, which comes to consolidate different scopes so as to prevent name conflicts.

A namespace is a local scope but of a different meaning. The syntax of a namespace (scope) is:

    namespace namespaceName
        {
            StatementList
        }

An example is:

    namespace first
        {
            int ident = 33;
            void fn();
        }

You saw this example in the previous part of the series.

Namespace Members
Each entity declared in the namespace is called a member. In the above case, ident and fn are members of the namespace called, first.

Namespace Content
The block of a namespace typically consists of declarations. It is not common to find definitions (of functions or class) in a namespace. Definitions are usually found in .cpp files and in libraries. Namespaces (blocks) are usually found in header files (see later). However, nothing stops you from having your own user-defined namespace in the file body of your program.

After declaring an entity in a namespace block, you just go ahead and use the name without any re-declaration, outside the block (but follow the rules of such use).

Effective Namespace Scope
Consider the following program that works:

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;
    }

When talking about effective scope, you talk of the portion or portions in the program where a name is usable. The name, identA is declared in the first line in the namespace, first. The name is usable from that line (point of declaration) to the end of the block, } of the namespace. After that just below, you have a portion with other statements where the name is not usable. Still going down below, you have the using-directive, “using namespace first;”. Below this directive, to the end of the file, the name is usable again. So, there are two portions in the program (file) where the name is usable. These two portions form the effective scope of the namespace, first. The second portion is as a result of the using-directive.

There is more to effective namespace scope than I have just said; you will get that in one of the next parts of the series. For now, take a break and meet me in the next part.

Chrys

Related Courses

C++ Course
Relational Database and Sybase
Windows User Interface
Computer Programmer – A Jack of all Trade – Poem
NEXT

Comments

Become the Writer's Fan
Send the Writer a Message