Namespace in C ++

Source: Internet
Author: User

Namespace in C ++

In enterprise-level development, a project is often very large, and many classes and methods are defined. In addition, many third-party libraries are introduced, this inevitably leads to conflicts of the same name. In Java, there is the concept of Package, that is, dividing classes with different functions into different packages to solve name conflicts, in C ++, namespaces are used to resolve such conflicts.

What is a name conflict?

Assume that your program has a third-party library ThirdLib. lib, which contains a global function void HelloWorld (). If you define another HelloWorld () in your project, a name conflict occurs.

For another example, A large project is developed by multiple people at the same time. Each person develops A module, including module A and Module B, respectively. Assume that module A has A struct structure Node and Module B also has A struct structure Node. Because it is developed by two people, Node A and Node B in module A have the same name but different structures. If neither module A nor Module B has A namespace, there will be A conflict, because there cannot be two same-lived classes in the same scope.

There are still many naming conflicts. In summary, multiple classes, functions, or variables with the same name appear in the same scope.

Define a namespace

[Example 1]

Hello. h

#pragma oncenamespace SH{class Hello{public:Hello(void);~Hello(void);public:void SayHello();};void HelloWord();}

Hello. cpp

#include "StdAfx.h"#include "Hello.h"#include 
 
  using std::cout;using std::endl;namespace SH{Hello::Hello(void){}Hello::~Hello(void){}void Hello::SayHello(){cout << "how are you!" << endl;}void HelloWord(){cout << "Hello World!" << endl;}}
 


Hello. cpp can also be defined as follows:

SH::Hello::Hello(void){}SH::Hello::~Hello(void){}void SH::Hello::SayHello(){cout << "how are you!" << endl;}void SH::HelloWord(){cout << "Hello World!" << endl;}

[Example 2] The namespace definition can also be discontinuous, for example:

Hello. h

#pragma oncenamespace SH{class Hello{public:Hello(void);~Hello(void);public:void SayHello();};}void HelloWord();namespace SH{void Hello2Sunly();}


Hello. cpp

#include "StdAfx.h"#include "Hello.h"#include 
 
  using std::cout;using std::endl;SH::Hello::Hello(void){}SH::Hello::~Hello(void){}void SH::Hello::SayHello(){cout << "how are you!" << endl;}void HelloWord(){cout << "Hello World!" << endl;}void SH::Hello2Sunly(){cout << "Hello Sunly!" << endl;}
 

In the above example, The namespace of the class Hello and the function Hello2Sunly is the same, and both are SH. HelloWord is not in the sh namespace and is in the default global namespace.

Use the namespace to add the prefix and scope of the namespace.

#include "stdafx.h"#include 
 
  int main(){std::cout << "Hello World!" << std::endl;return 0;}
 

This method is the safest, because each variable used uses the namespace prefix to specify a specific version. However, it is troublesome because a prefix is required for each use.


Using statement

#include "stdafx.h"#include 
 
  using std::cout;using std::endl;int main(){cout << "Hello World!" << endl;return 0;}
 

When a variable is needed, the variable is declared. This method is relatively safe and not too troublesome. Only one method is recommended. The scope of a variable introduced with using starts from the using indication until the end of the using indication.


Using indication

#include "stdafx.h"#include 
 
  #include "Hello.h"using namespace std;using namespace SH;int main(){cout << "Hello World!" << endl;Hello2Sunly();return 0;}
 

In this way, all the variables in a namespace are injected. This method makes programming easier, and you do not need to add the variables you need to use using one by one, however, it brings the danger of name conflicts again, because multiple namespaces are mentioned in the current scope, and the problem of duplicate names may occur.

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.