31. c ++ primer 4th notes, namespace

Source: Internet
Author: User

1The namespace can be divided into global namespaces. A namespace is a scope.

2The namespace can be defined within the global scope or other scopes, but not within the function or class. You can add any declaration that can appear in the global scope in the namespace.

3The namespace scope cannot end with a semicolon.

4Namespace is cumulative. The separated parts of a namespace can be dispersed in multiple files.

Example

 
Namespace namespace_name {}

You can either define a new namespace or add it to an existing namespace.

5The namespace definition can be non-consecutive, And the separated interface file and implementation file can be used to form the namespace. Therefore, you can use the same method as managing your own classes and function definitions to organize namespaces.

Example

Namespace cplusplus_primer {class sales_item {/*... */}; sales_item operator + (const sales_item &, const sales_item &); // declarations for remaining functions in the sales_iteminterface} // ---- query. h ---- namespace cplusplus_primer {class query {public: Query (const STD: string &); STD: ostream & Display (STD: ostream &) const; //...}; class query_base {/*... */};} // ---- sales_item.cc ---- # include "sales_item.h" namespace cplusplus_primer {// definitions for sales_item members and overloaded operators} // ---- query. CC ---- # include "query. H "namespace cplusplus_primer {// definitions for query members and related functions}

You can also define a namespace member in an external namespace definition, using a method similar to defining a class member outside the class.

6The name defined in the global scope (the name declared in any class, function, or namespace) is defined in the global namespace. The global namespace is implicitly declared and has no name.: Mem_nameReference a global namespace member.

7The namespace definition is partial to a specific file. It can be discontinuous in a given file and never span across multiple text files. Each file has its own namespace.

The names defined in an unnamed namespace can be found in the scope that defines the namespace.

If the header file defines an unnamed namespace, the names in the namespace define different local entities in each file that contains the header file.

This prevents static files from being replaced by an unnamed namespace.

8Header files should not containUsingInstructions orUsingStatement.

9, Namespace alias:

A namespace can have many aliases, and all aliases and the original namespaces can be used interchangeably.

Example

 
Namespace primer = cplusplus_primer;

10,UsingIndication andUsingStatement

Example

 
Namespace namespace_myname {int I;} using namespace namespace_myname; // using directiveusing namespace_myname: I; // using Declaration

ByUsingDeclarative errors are more likely to be detected and corrected because they are detected at the declarative point instead of vertices.

11Search for and class type parameters related to real parameters

Example

 
STD: String s; // OK: callstd: Getline (STD: istream &, const STD: string &) Getline (STD: Cin, S );

Functions that accept Class-type parameters (or class-type pointers and reference parameters) (including overload operators), and functions (including overload operators) defined in the same namespace as the class itself ), it is visible when the class Object (or class type reference and pointer) is used as the real parameter.

When the compiler seesGetlineFunction usageGetline (STD: Cin, S );When, It is in the current scope, including the scope and definition of the callCINAndStringType. Therefore, it is in the namespaceSTDFind and findStringType definedGetlineFunction.

12The name of a function with one or more class-type parameters contains the namespace of each type.

13, Overload andUsing directive, Declaration

1) Overload andUsingStatement

Example

Using ns: Print (INT); // error: cannot specify parameter listusing ns: Print; // OK: Using declarations specify names only

If the internal functions of the namespace are overloadedUsingThe Declaration declares all functions with the function name.

2) Overload andUsingInstructions

If the namespace function has the same name as the function declared in the namespace's scope, add the namespace member to the reload set.

Example

Namespace libs_r_us {extern void print (INT); extern void print (double);} void print (const STD: string &); // using directive: Using namespace libs_r_us; // using directive added names to the candidate set for callto print: // print (INT) from libs_r_us // print (double) from libs_r_us // print (const STD :: string &) declared explicitlyvoid foobar (INT ival) {print ("value:"); // callglobal print (const string &) print (ival); // calllibs_r_us :: print (INT )}

14, Namespace and template

The explicit and special template must be declared in the namespace that defines the general template. Otherwise, the special template will have different names than the specific template.

There are two methods to define the Special: one is to re-open the namespace and add the special definition (The namespace definition is not continuous); or, you can define the features in the same way as defining external namespace members in a namespace: Use the Template Name Defined by the namespace name to define the features.

To enable the customization of the template defined in the namespace, you must ensure that the customization is made in the namespace containing the original template definition.

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.