Interpretation of C + + namespaces "Go"

Source: Internet
Author: User
Tags modifier

The purpose of using namespaces is to localize the names of identifiers to avoid naming conflicts. In C + +, variables, functions, and classes exist in large numbers. Without namespaces, the names of these variables, functions, and classes will all exist in the global namespace, causing many conflicts. For example, if we define a function toupper () in our own program, this overrides the ToUpper () function in the standard library, because both functions are in the global namespace. Naming conflicts can also occur in cases where a program uses two or more third-party libraries. At this point, it is quite possible that the name in one of the libraries is the same as the name in the other library, which is a conflict. This situation often occurs on the name of the class. For example, we define a stack class in our own program, and a library in our program may have a class with the same name, and the name conflicts.

The advent of the Namespace keyword is aimed at this problem. Because this mechanism is localized for the names declared therein, the same name can be used in different contexts without causing a name conflict. Perhaps the biggest beneficiaries of namespaces are the standard libraries in C + +. Before a namespace occurs, the entire C + + library is defined in the global namespace (which is, of course, the only namespace). With the introduction of namespaces, the C + + library is defined in its own namespace, called Std. This reduces the likelihood of name collisions. We can also create our own namespaces in our own programs so that we can localize names that we think might cause conflicts. This is especially important when we are creating classes or libraries of functions.

Namespace Basics

The namespace keyword allows us to separate the global namespace by creating a scope. Essentially, a namespace defines a scope. The basic form of defining the namespace is as follows:

namespace name {//declaration}

Anything defined in a namespace is confined to that namespace.

Here is an example of a namespace where a class that implements a simple decrement counter is localized. A counter class is defined in this namespace to implement the count, where Upperbound and lowerbound are used to represent the upper and lower bounds of the counter.

Demo namespaces

  1. Namespace Counternamespace
  2. {
  3. int upperbound;
  4. int lowerbound;
  5. Class counter
  6. {
  7. int count;
  8. Public
  9. Counter (int n)
  10. {
  11. if (n <= upperbound) {
  12. Count = N;
  13. } else {
  14. Count = Upperbound;
  15. }
  16. }
  17. void Reset (int n)
  18. {
  19. if (n < upperbound)
  20. {
  21. Count = N;
  22. }
  23. }
  24. int run () {
  25. if (Count > Lowerbound)
  26. {
  27. return count--;
  28. } else {
  29. return lowerbound;
  30. }
  31. }
  32. };
  33. }

The Upperbound,lowerbound and class counter are all part of a namespace counternamespace definition scope.

An identifier declared in a namespace is a modifier that can be directly referenced and does not require any namespace. For example, in the Counternamesapce namespace, the run () function can refer to lowerbound directly in the statement:

Click (here) to collapse or open

    1. if (Count > Lowerbound)
    2. {
    3. return count--;
    4. }

However, since namespaces define a scope, we need to use the scope resolution operator to refer to the objects in the namespace outside of the namespace. For example, assigning a value of 10 to a upperbound outside the scope defined by the namespace Counternamespace must be written like this:

Counternamespace::upperbound = 10;

Or the object that you want to declare a counter class outside the scope of the Counternamespace definition must be written like this:

Counternamespace::counter obj;

In general, a member who wants to access the inside of a namespace outside the namespace needs to precede the member with the namespace and scope resolution operators.

The following program shows how to use the Counternamespace namespace:

Demo namespaces

  1. #include <iostream>
  2. using namespace Std;
  3. Namespace Counternamespace
  4. {
  5. int upperbound;
  6. int lowerbound;
  7. Class counter
  8. {
  9. int count;
  10. Public
  11. Counter (int n)
  12. {
  13. if (n <= upperbound)
  14. {
  15. Count = N;
  16. } else
  17. {
  18. Count = Upperbound;
  19. }
  20. }
  21. void Reset (int n)
  22. {
  23. if (n < upperbound)
  24. {
  25. Count = N;
  26. }
  27. }
  28. int run ()
  29. {
  30. if (Count > Lowerbound)
  31. {
  32. return count--;
  33. }
  34. Else
  35. return lowerbound;
  36. }
  37. };
  38. }
  39. int main ()
  40. {
  41. Counternamespace::upperbound = 100;
  42. Counternamespace::lowerbound = 0;
  43. Counternamespace::counter Ob1 (10);
  44. int i;
  45. do {
  46. i = Ob1.run ();
  47. cout << i << "";
  48. } while (i > Counternamespace::lowerbound);
  49. cout << Endl;
  50. Counternamespace::counter Ob2 (20);
  51. do {
  52. i = Ob2.run ();
  53. cout << i << "";
  54. } while (i > Counternamespace::lowerbound);
  55. cout << Endl;
  56. Ob2.reset (100);
  57. Do
  58. {i = Ob2.run ();
  59. cout << i << "";
  60. } while (i > Counternamespace::lowerbound);
  61. cout << Endl;
  62. return 0;
  63. }

Note: The counter class and the references to Upperbound and Lowerbound are preceded by a counternamespace modifier. However, once an object of type counter is declared, there is no need to use this modifier on any member of the object. So Ob1.run () can be called directly. The namespaces can be parsed.

The same space names can be declared multiple times, and such declarations are complementary to each other. This allows namespaces to be split into several files or even different places in the same file. For example:

Click (here) to collapse or open

    1. Namespace NS
    2. {
    3. int i;
    4. }
    5. //...
    6. Namespace NS
    7. {
    8. Int J;
    9. }

Where the namespace NS is divided into two parts, but the contents of the two parts are in the same namespace. i.e. NS. The last point: namespaces can be nested. This means that you can declare additional namespaces inside a namespace.

using Key Words

If you need to refer to a member of a namespace more than once in a program, it is cumbersome to use the scope resolver to specify the namespace each time, as we have previously said. To solve this problem, the Using keyword has been introduced. Using statements are typically used in two ways:

using namespace namespace name;

Using namespace name:: member;

The namespace name in the first form is the namespace we want to access. All members in the namespace are introduced into the current scope. That is, they are all part of the current namespace and are no longer required to use the scope qualifier. The second form simply makes the specified member in the specified namespace visible in the current scope. We use the preceding counternamespace for example, the following using statements and assignment statements are valid:

Using Counternamespace::lowerbound; Only the lowerbound is currently visible

Lowerbound = 10; This is legal because the Lowerbound member is currently visible

Using Counternamespace; All members of the Counternamespace space are currently visible

Upperbound = 100; This is legal because all the counternamespace members are now visible.

Here are the results of the changes we made to the previous program:

Use using

  1. #include <iostream>
  2. using namespace Std;
  3. Namespace Counternamespace
  4. {
  5. int upperbound;
  6. int lowerbound;
  7. Class counter
  8. {
  9. int count;
  10. Public
  11. Counter (int n)
  12. {
  13. if (n < upperbound)
  14. {
  15. Count = N;
  16. }
  17. Else
  18. {
  19. Count = Upperbound;
  20. }
  21. }
  22. void Reset (int n)
  23. {
  24. if (n <= upperbound)
  25. {
  26. Count = N;
  27. }
  28. }
  29. int run ()
  30. {
  31. if (Count > Lowerbound)
  32. {
  33. return count--;
  34. }
  1. Else
  2. {
  3. return lowerbound;
  4. }
  5. }
  6. };
  7. }
  8. int main ()
  9. {
  10. It's just a upperbound in Counternamespace.
  11. Using Counternamespace::upperbound;
  12. Access to Upperbound does not require the use of the range qualifier at this time
  13. Upperbound = 100;
  14. However, when using lowerbound, it is still necessary to use the range qualifier
  15. Counternamespace::lowerbound = 0;
  16. Counternamespace::counter Ob1 (10);
  17. int i;
  18. Do
  19. {
  20. i = Ob1.run ();
  21. cout << i << "";
  22. }while (i > Counternamespace::lowerbound);
  23. cout << Endl;
  24. Below we will use the entire Counternamespace namespace
  25. using namespace Counternamespace;
  26. Counter OB2 (20);
  27. Do
  28. {
  29. i = Ob2.run ();
  30. cout << i << "";
  31. }while (i > Counternamespace::lowerbound);
  32. cout << Endl;
  33. Ob2.reset (100);
  34. Lowerbound = 90;
  35. Do
  36. {
  37. i = Ob2.run ();
  38. cout << i << "";
  39. }while (i > Lowerbound);
  40. return 0;
  41. }
  42. The above program also demonstrates an important point: when we introduce a namespace using the using, if we have previously referenced other namespaces (or the same namespace), we will not overwrite the previous introduction, but rather supplement the previous introduction. That is, in the end, both the STD and Counternamespace namespaces in the above program become global spaces.
  43. Namespaces with no Name
  44. There is a special namespace called an unnamed namespace. This namespace with no name allows us to create namespaces that are available in a file scope. Its general form is as follows:
  45. Namespace
  46. {
  47. Statement
  48. }

We can use this unnamed namespace to create identifiers that are visible only in the file that declares him. That is, only in the file that declares the namespace, its members are visible, its members can be used directly, and no namespace names are required to be decorated. For other files, the namespace is not visible. As we mentioned earlier, restricting the scope of the global name to declaring his file is a way of declaring it static. Although C + + supports static global declarations, a better approach is to use the unnamed namespaces here.

STD name Space

Standard C + + defines its entire library in the Std namespace. This is why most of the programs in this book have the following code:

using namespace Std;

This is written in order to introduce the members of the Std namespace into the current namespace so that we can directly use the functions and classes in it, without having to write the STD every time::.

Of course, we can specify STD every time a member is used::, as long as we like. For example, we can display the following statement to specify cout:

Std::cout << "show using std:: to specify cout";

If our program uses only a small number of members in the Std namespace, or if the introduction of the Std namespace could lead to a namespace conflict, we would have no need to use the using namespace Std. However, if we are going to use the STD namespace members multiple times in the program, it would be much easier to introduce the members of the Std namespace into the current namespace by using namespace Std, rather than displaying the specified at the time of use alone.

Reprint Address: http://blog.renren.com/share/730973714/7874424429

Interpretation of C + + namespaces "Go"

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.