The namespace of C + +

Source: Internet
Author: User

The concept of no namespace in C is a new thing introduced in C + +, in order to handle conflicts of the same name.

Namespaces are actually a concept of encapsulation. Enclose some variables, functions, and classes. is similar to the encapsulation concept of a class , but larger than the encapsulation of a class.

First, analysis

1. Custom-defined namespaces

Format: namespacename{}

namespace my{       int i=2;       void foo ()       {cout<< "my:" <<i<<endl;}} namespaceyour{       class a{};}


2. Use something in a namespace (3 methods)

namespace my{       int i=2;       void foo ()       {cout<< "my:" <<i<<endl;}}

(1) using namespace my; After that the program can use everything in my (I and Foo ())

Using namespace My;int _tmain (int argc, _tchar* argv[]) {       foo ();//ok       Cout<<i<<endl;//ok       System ("pause");       return 0;}

(2) using My::foo, then the program can use my foo.

Using My::foo;int _tmain (int argc, _tchar* argv[]) {       foo ();//ok       Cout<<i<<endl;//error       System ("pause");       return 0;}

(3) Direct use of my::i and My::foo () in the program

int _tmain (int argc, _tchar* argv[]) {       my::foo ();//ok       Cout<<my::i<<endl;//ok       system ("Pause ");       return 0;}


Thinking: namespaces and classes are actually packaged in a similar way. A namespace in which you want to use something in another namespace must have a using or namespace::.

Second, deep analysis

1. Defining namespaces

Namespaces are cross-file. That is, you can define the same namespace in multiple CPP files, so that you can add something to a previously existing namespace.

namespacestd{

int i=2;

}

The purpose of this code is to add the variable I to the namespace Std. (although not advocated, but without grammatical errors)

In fact, the C + + standard library is doing the same. Because a namespace in the standard library contains a number of functions or classes, it is not possible to write these functions and classes in a CPP file. So use a namespace that continually adds things to a namespace to form a complete standard library.

2. Using namespaces

The first mentioned use of something in the namespace is that the definition of the namespace is already in this CPP, and if it does not exist, you need to include the definition of the namespace to use. For example: Inlcude<iostream>usingnamespace std;

See here a different question arises:

What is the difference between #include<xxx.h> in C and #include<xxx> using namespace XXX in C + +?

In fact, these 2 statements are want to use the things in XXX. There is no concept of namespaces in C, so namespace is not required. In C + +, the function of the standard library is placed in a namespace, so there must be a using namespace xxx;

That is to say, the various functions in iostream are in STD, obviously STD also includes many other things.

Think: why is C + + going to remove the. h from include<xxx.h>?

Because when we write C + + programs, we sometimes write the definition of the class in the. h file, and put the implementation of the class in the CPP file.

T.hclass a{public:void foo ();};

T.cpp#include "stdafx.h" #include <iostream>using namespace std; #include "t.h" void A::foo () {       cout<< "Print" <<ENDL;} int _tmain (int argc, _tchar* argv[]) {       a A;       A.foo ();       System ("pause");       return 0;}


Here in T.cpp, we include "t.h", we just want the definition of Class A.

And the above we talked about, C + + #include<xxx> we want to use XXX inside the thing. Obviously xxx is definitely not just a definition, and must have been realized.

So here C + + This change purpose is to distinguish between XXX and xxx.h. xxx is something that has been defined and implemented well. And xxx.h can be just a defining formula.

So we just have to see XXX, we can be sure that it is the definition of implementation. and to see xxx.h, it may just be a defining formula.

3. The essential meaning of include

It is simple to put the files behind the include in this file to compile and execute.

4. Global namespace

Any piece of code in C + + is in a namespace. All of the namespaces are not defined, and C + + defaults to the global namespace. Note that the global namespace and other namespaces are inherently indistinguishable.

namespace My{int I=1;} int i=2;using namespace My;int _tmain (int argc, _tchar* argv[]) {       i=3;//error       system ("pause");       return 0;}

Since i=2 is not defined by the namespace, it belongs to global namespace. The using namespace my, so the following main is in the global namspace, so you can use Globalnamespace, or you can use my namespace. Because there are two namespaces with I, the system does not know which one to call, and it goes wrong.

5. Nameless namespaces

namespace {

int I=1;

}

Obviously cannot be used by other files. It can only be used effectively in this document. Note the difference between the Globalnamespace and the.

6. Do not add anything to the standard library in C + +

For example, the above 1 mentions:

namespacestd{

int i=2;

}

If this code is referenced by another program, the code will not compile or run incorrectly. But in C + + there is the possibility of ambiguous behavior, and sometimes there are inexplicable things. So be sure not to add.

7. Nesting namespaces

Namespacensl
{const int rate=0.08;//constant
Double pay;//variable
Double tax ()//function
{return 1.2*rate;}
namespace NS2//nested namespaces
{int age;}
}
If you want to output the data for a member in the namespace NSL, you can use the following method:

cout<<nsl::rate<<endl;
COUT<<NSL::p ay<<endl;
Cout<<nsl::tax () <<endl;
cout<<nsl::ns2::age<<endl; //need to specify a named middle name for the outer and inner layers

The namespace of C + +

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.