C ++ tips: namespace

Source: Internet
Author: User

 

 

C developers often use # define, that is, macro to declare constants, but macros are global, which is difficult to maintain for large projects and often leads to name conflicts. Fortunately, C ++ brings us the namespace. It is used as follows. A namespace can group a group of logic, and the namespace is also a scope.

View plain
Copy to clipboard
Print
?
  1. Namespace
    Outspname
  2. {
  3. Const
     
    Int
    Cvar1 = 1;
  4. Const
     
    Char
    *
    Const
    Cvar2 =
    "33333"
    ;
  5. Void
    Test ();
  6. Namespace
    Inspname
  7. {
  8. Enum
    {A, B, c };
  9. Class
    Klass
  10. {
  11. };
  12. }
  13. }

Namespace outspname <br/>{< br/> const int cvar1 = 1; <br/> const char * const cvar2 = "33333"; <br/> void test (); </P> <p> namespace inspname <br/>{< br/> Enum {a, B, c }; <br/> class Klass <br/>{< br/>}; <br/>}< br/>}

 

However, even a simple namespace has many xuanjicang.

1. When a name is used out of its own space, it is repeatedly prefixed with a namespace, such

View plain
Copy to clipboard
Print
?
  1. Const
     
    Int
    Local = outspname: inspname:

Const int local = outspname: inspname:

 

This is not annoying. InWithin a small local scope,

We can use a declaration. For example

View plain
Copy to clipboard
Print
?
  1. {
  2. Using
    Outspname: inspname:;
  3. Const
     
    Int
    Local =;
  4. }

{<Br/> using outspname: inspname: A; <br/> const int local = A; <br/>}

 

2. Also, we can use a command to change all the names in the namespace to available. As shown below, there is a namespace behind using.Similarly, only using namesapce is used in a small local scope. Otherwise, the name will be contaminated.

View plain
Copy to clipboard
Print
?
  1. {
  2. Using
     
    Namespace
    Outspname;
  3. Const
     
    Int
    Local2 = cvar1;
  4. Const
     
    Int
    Local2 = inspname: B;
  5. {
  6. Using
     
    Namespace
    Inspname;
  7. Klass * P = new
    Klass ();
  8. }
  9. }

{<Br/> using namespace outspname; <br/> const int local2 = cvar1; <br/> const int local2 = inspname: B; <br/>{< br/> using namespace inspname; <br/> Klass * P = new Klass (); <br/>}</P> <p>}

 

However, when using namespace, pay attention to the following. For example, the testname: test method is declared in a. h file.

View plain
Copy to clipboard
Print
?
  1. Namespace
    Testname
  2. {
  3. Void
    Test (
    Int
    Param );
  4. }

Namespace testname <br/>{< br/> void test (int param); <br/>}

 

In its. cpp,The following method cannot be used. The test method is only a local method of the compilation unit, rather than the test method of the testname namespace.

View plain
Copy to clipboard
Print
?
  1. Using
    Testname;
  2. Void
    Test (
    Int
    Param)
  3. {
  4. }

Using testname; <br/> void test (int param) <br/>{< br/>}

 

The correct method is

View plain
Copy to clipboard
Print
?
  1. Namespace
    Testname
  2. {
  3. Void
    Test (
    Int
    Param)
  4. {
  5. }
  6. }

Namespace testname <br/>{< br/> void test (int param) <br/>{< br/>}< br/>}

 

Or

View plain
Copy to clipboard
Print
?
  1. Void
    Testname: Test (
    Int
    Param)
  2. {
  3. }

Void testname: Test (int param) <br/>{< br/>}

 

3. The namespace alias. When the namespace is very long or nested, we can use the namespace alias as follows:

View plain
Copy to clipboard
Print
?
  1. Namespace
    Oin = outspname: inspname;

Namespace oin = outspname: inspname;

 

4. An unnamed namespace is mainly used to preserve the code locality. The usage is as follows:

View plain
Copy to clipboard
Print
?
  1. Namespace

  2. {
  3. Const
     
    Int
    Cvar1 = 1;
  4. Void
    Test ();
  5. }

Namespace <br/>{< br/> const int cvar1 = 1; <br/> void test (); <br/>}

 

However, you must note that,In the implementation of the C ++ compiler, the unnamed namespace actually has a name. This implicit name is related to the name of the compilation unit in which it is located.
Therefore, we cannot use the name in the namespace across compilation units.

The above statement is equivalent

View plain
Copy to clipboard
Print
?
  1. Namespace
    $
  2. {
  3. Const
     
    Int
    Cvar1 = 1;
  4. Void
    Test ();
  5. }
  6. Using
     
    Namespace
    $;

Namespace $ <br/>{< br/> const int cvar1 = 1; <br/> void test (); <br/>}< br/> using namespace $;

Among them, $ has unique names in the scope where it is located, and the Unnamed namespaces in each compilation unit are also different, using namesapce $ is only the implicit name of the current compilation unit, soThe name in the namespace cannot be used across compilation units.

Assume that the test method is defined and implemented in A. H and A. cpp, but the test method or cvar1 cannot be used directly in B. H or B. cpp. In this compilation unit of B, the test symbol in the compilation unit of B is linked. It is not the test symbol in the compilation unit of A, and there will be uncertain symbols.

5. Avoid using a very short name in the namespace. It cannot be too long or nested too deeply. I personally think it should not exceed four layers.

 

Reprinted with the source: http://blog.csdn.net/szlanny/archive/2009/07/12/4341531.aspx

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.