Understanding and practice of namespace in C + + __c++

Source: Internet
Author: User
1. Foreword
Namespace Chinese meaning is a namespace or name space, the traditional C + + only a global namespace, but because of the scale of the current program is becoming more and more small, the global scope becomes more and more crowded, everyone may use the same name to achieve different libraries, As a result, programmers may have conflicting names when merging programs. namespace introduces complexityTo solve the problem. namespace allows like classes, objects, and functions to be clustered under a name. Essentially, namespace is a subdivision of the global scope.
A typical example:
#include <iostream>
using namespace std;

int main ()
{
    printf ("Hello World!");
    return 0;
}
This may be our first contact with namespace code. However, the namespace function is more than that. The basic format of the namespace format is:
Namespace identifier
{
    entities;
}
For example:
Namespace exp
{
    int a,b;
}
It's kind of like a class, but it's completely two different types.
In order to use the variables in namespace outside the namespace we use:: Operators, such as EXP::A, Exp::b. Using namespace can be an effective way to avoid redefining problems. 2. Avoid redefining problems
#include <iostream>
using namespace std;
namespace-i
{
  int var = 5;
}

namespace second
{
  double var = 3.1416;
}

int main () {
  cout << first::var << Endl;
  cout << second::var << Endl;
  return 0;
}
The result: 5 3.1416. Two global variable names are all Var, but they are not in the same namespace so there is no conflict. We can also see from here that the biggest effect of namespace is that we can solve the problem of duplicate definition well.
The 2.1 using can import the entire namespace
#include <iostream>
using namespace std;

Namespace
-a
  -{int x = 5;
  int y = ten;
}

namespace second
{
  double x = 3.1416;
  Double y = 2.7183;
}

int main () {
  using namespace-i;
  cout << x << Endl;
  cout << y << Endl;
  cout << second::x << Endl;
  cout << second::y << Endl;
  return 0;
}
The output is: 5,10,3.1416,2.7183; As we foresaw the entire first namespace of the import, the value of the previous pair x,y is the x,y value of a.
Here we cannot add a phrase "using namespace second" under "using namespace first;"
This is tantamount to a direct and complete disregard of namespace first and namespace second, which will result in duplicate definitions, so the preceding Hello_ There is a certain degree of problem with using directives in WORLD.C, simply because we use a namspace, and once a new namespace is introduced it is likely that the problem of duplication can occur. 2.2 Qualifying methods to support displayIn the header file, we usually insist on using the Explicit Qualificationand confine the using directives to very small scopes so that their usefulness is limited and easy to use. A similar example:

#include <iostream>
using namespace std;

Namespace
-a
  -{int x = 5;
}

namespace second
{
  double x = 3.1416;
}

int main () {
  {
    using namespace.
    cout << x << endl;
  }
  {
    using namespace second;
    cout << x << endl;
  }
  return 0;
}
Output is: 5, 3.1416; can see two different namespace are limited to different scopes {}, there is no conflict. 3. Reference materials[1] "C + + must know" [Beauty]stephen c.dewhurst, glorious translation
[2] Http://www.cplusplus.com/doc/tutorial/namespaces.html
[3] "The essence of C + +" [Beauty]ira Pohl Wang Shu-wu Chen Shao Translation
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.