Broad Network


C++ Miscellaneous Features

C++ Namespace - Part 5

Forward: In this part of the series I talk about C++ namespace features that do not easily fall into the previous groups.

By: Chrysanthus Date Published: 7 Feb 2013

Introduction

This is part 5 of my series, C++ Namespace. In this part of the series I talk about C++ namespace features that do not easily fall into the previous groups. You should have read the previous parts of the series before reaching here, as this is a continuation. You should have been reading the series in the order given.

using namespaceName::name
This heading is a syntax I would like you to know about. Consider the following program that works:

#include <iostream>
using namespace std;

namespace premier
    {
        int identA;
        int identB = 96;
        float func();
    }

int main()
    {
        using premier::identB;
        cout << identB << "\n";
        cout << identB + 31 << "\n";
        cout << identB * 2 << "\n";

        return 0;
    }

The first line in the main function is:

        using premier::identB;

It has the reserved word, using, followed by a namespace name, the scope resolution operator and then a name of an entity from the namespace. You use this using-syntax if you intend to use only one particular name below the using-directive; otherwise, you use the syntax, “using namespace namespaceName”.

Nesting Namespaces
A namespace can be nested inside another; the assembly of which can be nested in another namespace. Read and try the following program:

#include <iostream>
using namespace std;

namespace premier
    {
        int identA;
        int identB = 96;
        float func();
        namespace second
            {
                float alpha = 2.5;
                float beta;
            }
    }

int main()
    {
        using namespace premier::second;
        cout << alpha << "\n";

        return 0;
    }

In this program, the syntax to access the inner namespace is:

        using namespace outerNamespaceName::innerNamespaceName;

This syntax refers to a whole sub-namespace. Do no confuse between this and the one above, which refers to one particular name.

Extension of Global Namespace
The global namespace is the global scope. The global scope extends into all blocks in the file, including nested blocks. So the global scope extends into a local (user-defined) namespace. Read and try the following code:

#include <iostream>
using namespace std;

int AAA = 15;

namespace premier
    {
        int identA = AAA;
        int identB = 96;
        float func();
    }

int main()
    {
        using namespace premier;
        cout << identA << "\n";

        return 0;
    }

The global name, AAA has been seen in the namespace, premier.

Namespace spread in different Places
If you have a long namespace, you can spread it in different places of the same file or in different files. In the case of multiple files, you bring the files together using the #include directive (see later). Such spreading is allowed provided you do not type an entity name in more than one block. Read and try the following program, where the local namespace is spread as components in the global scope:

namespace premier
    {
        int ident0 = 100;
        float func0();
    }

namespace premier
    {
        int ident1 = 200;
        float func1();
    }

namespace premier
    {
        int ident2 = 300;
        float func2();
    }

int main()
    {
        using namespace premier;
        cout << ident0 << "\n";
        cout << ident1 << "\n";
        cout << ident2 << "\n";

        return 0;
    }

In the code, you have “three” local namespaces with the name, premier. Since all namespaces have the same name, they all form one namespace. No entity name should be retyped in any of the other blocks. The above sections (components) of the namespace can be typed in three different files of the same application.

Namespace Alias
When the name of a namespace is very long, you can use an alias name (short name) in that same way that you use all namespace names.  Consider the following program that works:

#include <iostream>
using namespace std;

namespace aVeryLongNameIndeed
    {
        int lng1 = 55;
        int lng2 = 66;
    }

namespace aliasNm = aVeryLongNameIndeed;

int main()
    {
        using namespace aliasNm;
        cout << lng2 << "\n";

        return 0;
    }

The original namespace name of aVeryLongNameIndeed is very long, so the alias name of aliasNm is adopted. The alias name is created after the namespace has been defined. The syntax is:

    namespace aliasNane = longNamespaceName;

That is it for this part of the series. We have come to the end of the series.

I assume you have completed this series. It means you already had the professional skills in C++. Now, you are in an advanced course of C++. You may already be working. If you ever have too much work, you can subcontract (outsource) some or all to someone in the site below.

Chrys

Programming Solutions

Related Courses

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

Comments

Become the Writer's Fan
Send the Writer a Message