Chapter 8 tools for large programs (8)

Source: Internet
Author: User

6. Define namespace members

Functions defined in a namespace can be abbreviated as names defined in the same namespace.

// Class2.cpp
# Include "stdafx. h"
# Include "NameSpace2.h"
 
Namespace andsoft
{
Namespace Namespace2
{
Class Class2 {
Private:
Class1 * c1; // Class1 can be referenced directly.
};
}
}
// Class2.cpp
# Include "stdafx. h"
# Include "NameSpace2.h"

Namespace andsoft
{
Namespace Namespace2
{
Class Class2 {
Private:
Class1 * c1; // Class1 can be referenced directly.
};
}
} You can also define a namespace member in an external namespace definition, in a way similar to defining a class member in a class: The namespace Declaration of the name must be in the scope, the namespace to which the name belongs must be specified.

// Class2.cpp
# Include "stdafx. h"
# Include "NameSpace2.h"
 
Class randsoft: Namespace2: Class2 {
Private:
Class1 * c1;
};
// Class2.cpp
# Include "stdafx. h"
# Include "NameSpace2.h"

Class randsoft: Namespace2: Class2 {
Private:
Class1 * c1;
};
7. You cannot define members in irrelevant namespaces.

Although the namespace member can be defined externally in The namespace definition, there are still some restrictions on this definition. Only the namespace containing the member declaration can contain the definition of the member.

8. global namespace

The name defined in the global scope (the name declared outside the namespace of any class, function, or namespace) is defined in the global namespace. The global namespace is implicitly declared and exists in each program. In the global scope, each object is defined to add those names to the global namespace.

You can use the scope operator to reference members of a global namespace. Because the global namespace is implicit and has no name, the mark: member_name references the members of the global namespace.

: Handle <int> h (new int (10 ));
: Handle <int> h (new int (10); 17.2.2 nested namespace

A nested namespace is a nested scope-its scope is nested within the namespace containing it. The name defined inside the nested namespace is partial to the namespace. Code other than the external namespace can only reference the name in the nested namespace by specifying the name.

Nested namespaces can improve the Code organization in the library.

// Namespace2.h
# Ifndef NAMESPACE2_H
# Define NAMESPACE2_H
Namespace andsoft
{
Namespace Namespace2
{
Class Class1;
Class Class2;
}
}
# Endif
// Namespace2.h
# Ifndef NAMESPACE2_H
# Define NAMESPACE2_H
Namespace andsoft
{
Namespace Namespace2
{
Class Class1;
Class Class2;
}
}
# Endifusing namespace std;
Using namespace Anders: NameSpace2;
 
Int _ tmain (int argc, _ TCHAR * argv [])
{
Anders: NameSpace2: Class1 c1 ();
Return 0;
}

From xufei96's column
 

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.