C + + namespaces

Source: Internet
Author: User

1. Namespaces are designed to resolve variables of the same name in different files.

For example, you add the using namespace std;
Then the STD namespace does not need to add std:.
and iostream.h This header file is to be compatible with C,c no namespace this concept, so do not add using namespace std;
If a popular way to explain is that a school, there are many classes, in the search for 1th students, in the end is to find which class of 1th?
Using namespace high one by one classes;
So when looking for all the high class one by one students, you do not have to enter the high one by one classes:: Number One
You don't have to write.
Using high Class One by one:: a number;
Using high one by one class:: Second;

Just write a number and you can do it.

Note that many programmers do not use the using namespace STD, because many variables are defined in the standard namespace STD and are accidentally duplicated with their own defined variables, so what can be used is to add:
Using Std::cin;
Using Std::cout;
Using Std::endl;
We can.

2. Team Work Development

In fact, this is still very useful, especially in team development, for example, you put the module to everyone to do, a wrote a class W,b also wrote a Class W, but because there was no good communication, and finally their class name, integration together, when the call back to the conflict. If you use a namespace, it is good to do, call, as long as it is called a class W or B class W to solve the problem.
such as: namespace A{class w{...};}
namespace b{class w{...};}
When used:
A::W AW//Generate the object of W written by a
Aw.get () .....
B::W bw//Generate B objects written by W
Bw.get () ....
This distinguishes the class from who it is.

------------------------------------------------------

namespace Peking//Declaration namespace Peking
{
int rank=47;//identifier
}

namespace Tsinghua//Declaration namespace Tsinghua
{
int rank=54;//with the same identifier as Peking
}

----------------------------------

#include "stdafx.h"
#include <iostream>

#include "university.h"//Load Header file University.h
UsingNamespace peking;//explicitly uses the namespace in the header file University.h Peking

int main ()
{
std::cout<< "World University Rankings" <<std::endl;

std::cout<< "Peking University:" <<rank<<std::endl;//rank equivalent to Peking::rank
std::cout<< "Tsinghua University:" <<tsinghua::rank<<std::endl;//identifier rank preceded by the namespace Tsinghua and the scope operator "::"

Return0;
}

------------------

Blog reference [http://www.cnblogs.com/assemble8086/archive/2011/09/30/2196152.html

Http://www.cnblogs.com/CaiNiaoZJ/archive/2011/08/20/2146917.html

]

C + + namespaces

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.