C + + namespace collation __c++

Source: Internet
Author: User
Tags function definition function prototype cmath

1.using: In order to avoid using namespaces every time, it is necessary to define a namespace by using the use of the content within the namespace for a specific area of the declaration using A::t;//using declaration

Use it again to invoke the name directly without increasing the space limit

2.using: Use as a compilation instruction to make all content in the corresponding namespace available and omit the scope resolution operator when used

using namespace A;

Precautions:

First, avoid ambiguity using a::t;

Using B::t;

t=1://which one.

Two, using do compile instruction when the namespace is global if local and namespace name is the same, local overwrite namespace content

Third, a local name overrides the namespace content of the global and using directives but can be distinguished using the T (local):: T (Global) A::t (namespace)

Four, the using declaration is more secure than the using compiler.

The declaration is the top-specific content compiler emits instructions that the compiler will not issue a warning when a compilation instruction imports all namespace content that might conflict overwrite.

3. Introduce a base class member name using a using declaration in a subclass (see C + + Primer)

When private or protected inherits, the access level of the base class member is more restricted in the derived class:

Class Base {

Public

std::size_t size () const {return n;}

Protected

std::size_t N;

};

Class Derived:private Base {...};

In this inheritance hierarchy, the member function size is public in Base, but is private in Derived. To make the size public in Derived, you can Derived public in the

Section to add a using declaration. The following changes the definition of Derived so that the size member can be accessed by the user and that n can be accessed by Derived derived classes:

Class Derived:private Base {

Public

Using Base::size;

Protected

Using Base::n;

// ...

};

In addition, when a member function in a subclass has the same name as a base class, a redefined member function in a subclass hides the version in the base class, even if the function prototype is different (see below for hidden conditions).

If a member function in a base class has multiple overloaded versions, a derived class can redefine 0 or more versions that are inherited, but only those versions that are redefined in a derived class are accessible through a derived type, so if the derived class wants to use all overloaded versions through its own type, the derived class must either redefine all overloaded versions. Either one is not redefined. Sometimes a class needs to redefine the behavior of only some versions of an overloaded set, and to inherit the meaning of other versions, in which case it can be annoying to redefine each base class version to redefine a specific version that needs to be customized. You can provide a using declaration for an overloaded member name in a derived class (a using declaration for a base class member function name to add all overloaded instances of that function to the scope of the derived class) so that the derived class does not have to redefine each of the base class versions that are inherited. A using declaration can only specify a name, cannot specify a formal parameter list, and after using a using declaration to add a name to the scope, the derived class only needs to redefine those functions that this type does have to define, and you can use the inherited definition for other versions.

"Hide" means that a function of a derived class masks a base class function with the same name as the following rule:

1, if the function of the derived class has the same name as the function of the base class, but the parameters are different. At this point, the function of the base class will be hidden regardless of the virtual keyword (Note that the overload is not confused)

2. If the function of the derived class has the same name as the function of the base class, and the parameters are the same, the base class function has no virtual keyword. At this point, the function of the base class is hidden (note that it is confused with overrides)

#include "StdAfx.h"

#include <iostream>

using namespace Std;

Class Base

{

Public

void Menfcn ()

{

cout<< "Base function" <<endl;

}

void MENFCN (int n)

{

cout<< cout<< "Base function with int" <<endl;

}

};

Class Derived:base

{

Public

A using Base::menfcn;//using declaration can only specify a name and cannot have a formal parameter list

int MENFCN (int)

{cout<< cout<< "Derived function with int" <<endl;}

};

int main ()

{Base B;

Derived D;

B.MENFCN ();

D.MENFCN ()//If you remove the using declaration from the Derived class, an error occurs: Error C2660: ' DERIVED::MENFCN ': function does not take 0 arguments Gnore (Std::cin.gcount () +1);//Empty buffer std::cin.get ();/Suspend program execution

}

=========================================================================================================

First, why the need for namespaces (issues raised)

Namespaces are user-named scopes introduced by ansic++ to handle common conflicts of the same name in programs.

There are 3 levels of scopes defined in the C language, that is, files (compilation units), functions, and compound statements. C + + also introduces a class scope, which appears in the file. Variables of the same name can be defined in different scopes, and the system can distinguish them from each other.

1, the scope of global variables is the entire program, in the same scope should not have two or more entities with the same name (enuty), including variables, functions and classes.

Example: If two classes are defined in a file, there can be functions with the same name in these two classes. When referencing, for the sake of distinction, the class masterpiece should be added to the qualification:
Class a //Declare Class A
{ public:
 void funl ();//Declare the FUNL function in Class A
private: &NBSP
 int i;
void A::funl ()  //to define the FUNL function
{...} in Class A. The

Class b //declares that class B
{ public:
 void funl (), and  //b classes also have function
void funl ();
 void B::FUNL ()  //defines the FUNL function in class B,
{...}
This does not confuse.

You can define a global variable in a file, which is scoped to the entire program. If a variable a int a=3 is defined in file A;
In file B, you can define a variable a int a=5;
There is no problem when compiling file A and file B separately. However, if a program includes file A and file B, an error is reported when the connection is made, because there are two variables with the same name in the same program, which is considered a duplicate definition of the variable.
You can declare by extern that a variable of the same name in the two files in the same program is the same variable. If you have the following declaration in file B:
Extem int A;
Indicates that variable A in file B is a variable that has been defined in other files. Because of this declaration, the scope of variable A of file A is extended to file B after the program has been compiled and connected. If you no longer assign a value to a in file B, the value of variable A in file A is output in file B: cout<<a;//Gets a value of 3
2, the program will appear name conflict.
In a simple program design, as long as people pay careful attention, you can strive for no errors. However, a large application software, often not by a single person to complete, but by a number of people to work together, different people to complete different parts, the final combination of a complete program. If different people define classes separately, put them in different header files, and when the primary file (the file containing the main function) needs to use these classes, the headers are included with the #include command line. Since the headers are designed by different people, it is possible to name the defined class or function in different header files with the same names.

Example 4 name conflict

Programmer A in the header file Headerl. Class student and function fun are defined in H.

The header file Header1 in Example 4 (header file 1, no file name is Cc8-4-h1.h)
#include <string>
#include <cmath>
using namespace Std;
Class Student//Declaration Student Classes
{public:
Student (int n,string nam,int a)
{num=n;name=nam;age=a;}
void Get_data ();
Private
int num;
String name;
int age; };
void Student::get_data ()//member function definition
{cout<<num<< "" <<name<< "" <<age<<endl;}
Double Fun (double a,double b)//defines global functions (that is, external functions)
{return sqrt (a+b);}

Include the header file Headerl.h in the file where the main function resides:
#include <iostream>
using namespace Std;
#include "Header1.h"//note to use double quotes, because files are generally placed in the user directory
int main ()
{Student stud1 ("Wang", 18);//define Class object Studl
Stud1.get_data ();
Cout<<fun (5,3) <<endl;
return 0; }

Program can run normally, output as
18 Wang
2.82843

If programmer B writes the header file Header2. H, in addition to defining other classes, also defines the class student and function fun, but its contents are headerl with the header file. The student and function fun in H are different.

Header file Header2 in Example 4
#include <string>
#include <cmath>
using namespace Std;
Class Student//Declaration Student Classes
{public:
Student (int n,string nam,char s)//parameter differs from Student in Headerl
{num=n;name=nam;sex=s;}
void Get_data ();
Private
int num;
String name;
char sex; };//This item is different from Headerl

void Student::get_data ()//member function definition
{cout<<num<< "" <<name<< "" <<sex<<endl;}

Double Fun (double a,double b)//define global functions
{return sqrt (a-b);} The return value is different from the fun function in Headerl
There may be other content in header file 2

If the master programmer uses Headerl in its program. The student and function fun in H, thus including the header file Headerl in the program. H, at the same time to use the header file Header2. Some of the content in H (but for Header2. h contains no knowledge of classes and functions with the same name as the student class and the fun function in headerl.h, because there are often many different pieces of information in a header file, and users tend to care only about the parts they need without paying attention to other content. Thus, the header file Header2 is included in the program. H. If the primary file (the file that contains the main function) is as follows:

#include <iostream>
using namespace Std;
#include "Header1.h"//Include header file L
#include "header2.h"//Include header file 2
int main ()
{Student stud1 ("Wang", 18);
Stud1.get_data ();
Cout<<fun (5,3) <<endl;
return 0; }

Then the program compiles and there is an error. Because after the precompilation, the contents of the header file replace the corresponding #include command line, so that there are two student classes and two fun functions in the same program file, which is clearly a duplicate definition, which is the name conflict, in which there are two or more entities with the same name in the same scope.

3, global namespace pollution (namespace pollution).
In the program also often need to refer to some libraries (including C + + compiler system provided by the library, the software developers provided by the library or the user's own development of the library), for this need to include the relevant header files. If the libraries contain entities with the same name as the global entity of the program, or if there are identical entity names in different libraries, a name conflict occurs at compile time.

In order to avoid the emergence of such problems, many methods have been proposed, for example: the name of the entity is written long-some (contains more than 10 or dozens of letters and characters); The name is special, including some special characters; The internal global identifier provided by the compilation system is prefixed with an underscore, such as _complex (). To avoid the same name as the entity named by the user; the name of the entity provided by the software developer is prefixed with a specific character. But this effect is not ideal, but also increased the difficulty of reading procedures, readability reduced.

The C language and the early C + + language did not provide an effective mechanism for solving this problem, and did not allow the library's providers to build their own namespaces. It is hoped that the ANSI C + + standard solves this problem by providing a mechanism and a tool that distinguishes the global identifier named by the designer of the Library from the global entity name of the program and the global identifier of other libraries.

Ii. What is a namespace (solution)

Namespaces: In fact, an area of memory that is named by a program designer, the programmer can specify some namespace domains with names as needed, and place some global entities in each namespace, separating them from other global entities.

such as: namespace NS1//designated Middle NSL
{int A;
Double b; }

namespace is a keyword that must be written to define a namespace, NSL is the name of the namespace that the user has specified (you can use any legal identifier, which is NS1 because the NS is the abbreviation for namespace, meaning please), and within the curly braces is the declaration block, The entity declared in it is called a namespace member (namespace). Namespace members now include variables A and B, noting that A and B are still global variables, simply hiding them in the specified namespace. If you want to use variables A and B in your program, you must add the namespace name and scope resolution "::", such as nsl::a,nsl::b. This usage is called a namespace qualifier (qualified), and these names (such as nsl::a) are called qualified names (qualified name). The role of namespaces in C + + is similar to the directory and file relationships in the operating system, because many files, inconvenient to manage, and easily duplicate the name, so people set up a number of subdirectories, the files are placed in different subdirectories, the different subdirectories of the file can have the same name. The file path should be indicated when the file is called.

The role of namespaces is to create some separate scopes, separating some global entities. Lest there should be an old point called Lee Sang, 3 people stand up to answer, this is the name conflict, because they can not tell what the teacher want to call the Lee Sang, the same name can not distinguish between. To avoid confusion of the same name, the school divided 3 students with the same name in 3 classes. In this way, when a small class name Lee Sang, only one person will answer. In other words, the name is unique within the class scope (i.e. class scope). If the headmaster call in the whole school, need to look for this student in the entire school area, need to consider the scope problem. If the headmaster called Lee Sang, there will be 3 students in the whole school shouting "to", because there are 3 students with the same name in the same scope. In order to differentiate the 3 students in the whole school, the headmaster must add the class number before the name, such as the Lee Sang of the senior class, or the Lee Sang of Class B, that is, the class name is limited. This will not lead to confusion.

You can set many namespaces as needed, each of which represents a different namespace field, and different namespaces cannot have the same name. In this way, the entities in different libraries can be placed in different namespaces, or, in different namespaces, different entities are concealed. The global variables we used in the past can be understood as global namespaces, independent of all well-known namespaces, that are not required to be declared with namespace, are actually implicitly declared by the system, and exist in each program.

When declaring a namespace, you can include not only variables in curly braces, but also the following types:

• Variables (can be initialized);
constant
• Number (can be defined or declared);
• Structural body;
Class
Template
• Namespaces (a namespace is defined in a namespace, which is a nested namespace).

For example

namespace NSL
{const int rate=0.08;//Constants
Doublepay//variable
Doubletax ()//function
{return a*rate;}
NAMESPACENS2//nested namespaces
{int age;}
}
If you want to output data for a member in a namespace NSL, you can use the following method:

cout<<nsl::rate<<endl;
COUT<<NSL::p ay<<endl;
Cout<<nsl::tax () <<endl;
cout<<nsl::ns2::age<<endl;//need to specify the middle name of the outer and inner layers

You can see that namespaces are declared and used in the same way as classes. But there is a difference: when declaring a class, there is a semicolon behind the right curly brace, and there is no semicolon behind the curly brace when defining the namespace.

Third, using namespaces to resolve name conflicts (use guide)

With the above foundation, namespaces can be used to solve the name conflict problem. Now, modify the example 4 program to make it work correctly.

Example 5 uses namespaces to resolve example 4 program name conflict issues.
Modify two header files to place the classes declared in the header file in two separate namespaces.
In example 8.5, header file 1, file name is Header1.h

using namespace Std;
#include <string>
#include <cmath>
namespace ns1//Declaration namespace NS1
{class Student//declaration of Student class within namespace NSL
{public:
Student (int n,string nam,int a)
{num=n;name=nam;age=a;}
void Get_data ();
Private
int num;
String name;
int age; };
void Student::get_data ()//Defining member functions
{cout<<num<< "" <<name<< "" <<age<<endl;}

Double Fun (double a,double b)//define fun function in namespace N-citation
{return sqrt (a+b);}
}

In example 8.5, header file 2, file name is Header2.h
#include <string>
#include <cmath>
namespace NS2//Declaration namespace NS2
{Class Student
{public:
Student (int n,string Nam,char s)
{num=n;name=nam;sex=s;}
void Get_data ();
Private
int num;
String name;
char sex; };

void Student::get_data ()
{cout<<num<< "" <<name<< "" <<sex<<endl;}

Double Fun (double a,double b)
{return sqrt (a-b);}
}

Main file
#include <iostream>
#include "Header1.h"//Include header file L
#include "header2.h"//Include header file 2
int main ()
{ns1::student stud1 ("Wang", 18);//define the Student class declared in the namespace NSL Studt
Stud1.get_data (); Do not write Ns1::studl.get_data ();
Cout<<ns1::fun (5,3) <<endl; Call the fun function in the namespace ns1
Ns2::student stud2 (102, "Li", ' f '); Defined with the student class declared in the namespace ns2 stud2
Stud2.get_data ();
Cout<<ns2::fun (5,3) <<endl; Call the namespace NSL, the fun function in
return 0; }

The key to solving this problem is to set up two namespaces NSL and NS2, and put classes that originally sound in two header files into namespaces NSL and NS2 respectively. Note: In the header file, do not place the #include command in the namespace, as you can see in the description in the previous section that the contents of the namespace do not include the command line, otherwise the compilation will fail.

The reason for parsing Example 4 program error is that there is the same class name student and the same function name fun in the two header files, and there is a name conflict when they are included in the main file, and there is a duplicate definition. The compilation system cannot tell which header file student to define the object studl. Now two student and fun are placed in separate namespaces, each of which has its own scope and is irrelevant. Because the scope is not the same, will not produce: a name conflict. Just as you can have variables and functions with the same name in two different classes, you don't have a conflict.

The studl is defined with Ns1::student (Student in the namespace NSL) when the object is defined, and ns2::student is defined with NS2 (Student in the namespace Stud2). Obviously, Nsl::student and Ns2::student are two different classes that don't create confusion. Similarly, namespace name NS] or NS2 are required to qualify when calling the fun function. Ns1::fun () and Ns2::fun () are two different functions. Note: Object studl is defined with Nsl::student, but the object studl is not in the namespace NSL. The scope of the studl is scoped to the main function. You should write studl when you call the member function Get_data of the object studl. Get_data () should not be written as Nsl::studl.get_data ().

The program can be successfully compiled and the following results are obtained:
Wang L9 (data in Object studl)
2.82843 (value of/5+3)
102 Li F (data in Object STUDG)
1.41421 (/5-2 of the value)

Iv. methods of using namespace members

As you can see from the above introduction, when referencing namespace members, namespace members are qualified with namespace names and scope specifiers to distinguish between identifiers of the same name in different namespaces. That

Namespace name:: namespace member name

This method is effective in ensuring that the referenced entity has a unique name. However, if the namespace name is longer, especially in the case of namespace nesting, a long name is required to refer to an entity. It may be inconvenient to refer to a namespace member more than once in a program.

1. Using namespace aliases

You can use an alias (namespace alias) for a namespace to replace a longer namespace name. Such as

Namespace Television//declares the namespace, named television
{ ... }

You can replace it with a short, easy to remember alias. Such as:

namespace Tv=television//alias TV is equivalent to the original television

It can also be said that the alias TV pointed to the original television, in the former television position can be unconditionally replaced by TV.

2. Use namespace member name

The namespace member name after using must be a namespace-qualified name. For example:

Using Nsl::student;

The above statement declares that the member student in the namespace ns1 is used in this scope (the scope in which the using statement is located), and if you use the namespace member in this scope, you do not have to use namespace qualification. For example, after using the using declaration above, the Student appearing in the subsequent program is implicitly referring to Nsl::student.

The valid scope of a using declaration is from the start of the using statement to the end of the scope where the using is located. If you have the following statement after the using statement above:

Student studl ("Wang", 18);//Here the Student equals ns1::student

The above statement is equivalent to

Nsl::student studl ("Wang", 18);

And AS

The fun that appears after the using Nsl::fun//declaration is within the namespace NSL fun
Cout<<fun (5,3) <<endl;//The fun function here is equivalent to Nsl::fun (5,3)

Obviously, this avoids using namespace qualification every time that you reference a namespace member, making referencing namespace members easy to use.
Note, however, that you cannot have members of the same name in a member of a different namespace with a using declaration in the same scope. For example:

Usmgnsl::student///declaration The Student that appears later is the Student in the namespace NSL
Usmgns2::student//Declaration The Student that appears later is the namespace ns2 small Student

Student STUD1//What is the name of the Student in the middle of the Student?

Generated two semantics, compilation error.

3. Use namespace namespace name

With the using namespace member name described above, only one namespace member can be declared at a time, and if 10 entities are defined in one namespace, you need to use a 10-time using namespace member name. Can you declare all members of a namespace once in a program with one statement?

C + + provides a using namespace statement for this purpose. The general format of a using namespace statement is

using namespace namespace name;

For example

Using Nanlespace NSL;

Declares that a member in a namespace NSL is to be used in this scope and that no namespace qualification is required when any member of the namespace is used. If you make the statement above, you have the following statement:

Student studl ("Wang",);//student implicitly refers to the Student in the middle NSL
Cout<<fun (5,3) <<endl;//The fun function here is the fun function in the named Middle NSL

In scopes declared with Usmgnamespace, the members of the namespace NSL are as if they were declared in the global domain. Therefore, you do not have to qualify with namespaces. Obviously this kind of processing is more convenient for writing programs. However, if you use UsingNamespace to declare multiple namespaces at the same time, you are often prone to error. The main function in Example 5, if replaced with the following program segment, will make an error.

int main ()
{using namespace nsl;//declares that members in NSL are available in this scope
A using namespace ns2;//declares that members in NS2 are available in this scope
Student studl ("Wang", 18);
Studl.8ct_data ();
Cout<<fun (5,3) <<endl;
Student stud2 (102, "Li", ' R ');
Stud2.get_data ();
Coutt<<fun (5,3) <<endl;
return O;}

Because two namespaces NSL and NS2 are introduced in the same scope, there are classes and functions with the same name. In the presence of student, can not determine which namespace of the student, there are two semantic, compilation error. Therefore, use namespace statements are used only if the number of namespaces is very small and if there are no members of the same name in those namespaces.

V. Nameless namespaces

As described above is a namespace with a name, C + + also allows namespaces with no name, such as the following unnamed namespaces declared in file a:

namespace//namespace has no Name
{void fun ()//Defining namespace members
{cout<< "OK. "<<endl;}
}

Because namespaces are not named and are clearly not referenced in other files, they are only valid within the scope of this file. Members of nameless namespaces the scope of the fun function is file a (specifically, from the point where the unnamed namespace is declared to the end of file a). Using a member of a nameless namespace in file A does not have to (and cannot) be qualified with a namespace name.

If you have the following statement in file a:
Fun ();
The member fun function in the nameless namespace is executed, and the output "OK." ”。

The fun function is also not available in other files in this program, that is, limiting the scope of the fun function to this file. It can be thought that a function can be declared with static in C, and that the function is scoped to this file. C + + retains the use of static declaration functions, while providing a nameless namespace for this functionality. As more and more C + + compiler systems implement the proposed namespace mechanism for ANSI C + +, it is believed that the method of using unnamed namespace members replaces the previously used static declarations of global variables.

Vi. Standard-named space std

To resolve conflicts of the same name between identifiers in the C + + standard library and the global identifiers in the program, as well as identifiers in different libraries, you should define (or declare) the identifiers of different libraries in different namespaces. All identifiers for the standard C + + library are defined in a namespace called STD, or the functions, classes, objects, and class templates in a standard header file (such as iostream) are defined in a namespace Std. STD is the abbreviation of standard (standard), which means that this is the namespace of the relevant content of the standard library, meaning please Chu, do not have to rote.
In this way, you need to use STD as a qualification when using the C + + standard library in your program. Such as

std::cout<< "OK. "<<endl;//Declaration cout is a stream object defined in the namespace Std

You can see this usage in some C + + books. However, it is obviously inconvenient for each cout,cm and other identifiers defined in Std to be qualified with a namespace Std. In most C + + programs, the Usmgnamespace statement is used to declare a namespace STD so that you do not have to process each namespace member one at the beginning of the file by adding the following using namespace declaration:

using namespace Std;

In this way, all identifiers defined and declared in STD can be used as a global amount in this file. However, it should be absolutely guaranteed that an identifier with the same name as the member of the namespace STD is not present in the program, for example, an object named cout cannot be defined in the program. Because there are too many entities defined in the namespace STD, and sometimes the program designer does not ask which identifiers have been defined in the namespace STD, some professionals prefer to use several "using namespace member" declarations instead of "using namespace namespaces" in order to reduce the chance of error. declarations, such as

Using Std::string;
Using Std::cout;
Using Std::cin;

Wait To reduce the use declarations that are repeated in each program, the program developer often makes a header file of the namespace STD member's USMG declaration that is often used to write the application, and then includes the header file in the program.

If you read a variety of books that introduce C + +, you may find that some books have a using namespace statement in their programs, while others do not. Some readers will ask: should there or should not? It should be said that in standard C + + programming, the members of the namespace STD should be declared or qualified (you can take any one of the methods described earlier). But most of the C + + libraries currently in use were developed a few years ago, and there was no namespace, and the relevant content in the library was not in the Std namespace, so there was no need to declare an STD in the program.

Vii. use of early function libraries

C Language Program in a variety of functions are basically implemented by the function, in the development of the C language has established a function of a rich library, C + + from the language inherited this valuable wealth. You can use the C language function library in C + + programs.

If you want to use a function in a function library, you must include the header file in the program file, and the declaration of the different functions in a different header file.

There are two ways to use these header files in C + +.

1, using the traditional method of C language
The header file name includes a suffix. h, such as STDIO.H,MATH.H. Because the C language has no namespaces, header files are not stored in namespaces, so if you use a suffix in a C + + program file. h header file, you do not need to use a namespace. Just include the header file in the file. Such as

#include <math. H>

2, the use of C + + new method
The C + + standard requires that the system-supplied header file does not include a suffix. h, such as iostream, String. In order to indicate a link to a C-language header file, the header file name used by C + + is the corresponding header file name in the C language (but does not include the suffix). h) before adding a letter C. For example, the header file name for input and output in the C language is stdio. h in C + + the corresponding header file name is Cstdio. C language in the header file math.h, in C + + corresponding header Wensch named Cmath. The header file string in the C language. h in C + + the corresponding header file name is CString. Note in C + +, header file cstnng and header file strmg are not the same file. The former provides a declaration of the related functions of string processing in C language (such as strcmp,ctrcpy), which provides new functionality for string processing in C + +.

In addition, because these functions are declared in the namespace STD, the namespace STD is declared in the program. Such as:

#include <cstdio>
#include <cmath>
using namespace Std;

Most C + + compiler systems currently in use both retain the use of C, and provide a new method of D C + +. The following two usages are equivalent and can be optional.

C Traditional Method C + + new method
#include <stdio.h> #include <cstdio>
#include <math.h> #include <cmath>
#include <string.h> #include <cstring>
using namespace Std;

You can use the traditional C method, but you should promote new methods of using C + +.

=========================================================================================================

Translation of C + + official website

A namespace is a declarative region that provides a scope for its internal identifiers (names of types, functions, and variables). Namespaces are used to organize code into logical groups and can also be used to avoid name collisions, especially if the codebase includes multiple libraries. All identifiers within a namespace range are visible to each other without any restrictions. An identifier outside of a namespace can access a member by using the fully qualified name of each identifier (for example, std::vector<std::string> VEC;), or through the using declaration of a single identifier (using std::string) or the using directive (C + +) of all identifiers in the namespace (using namespace std;) To access the members. The code in the header file should always use the fully qualified namespace name.

The following example shows three methods that code outside of a namespace declaration and namespace can access its members.

Namespace Contosodata
{    
    class ObjectManager 
    {public
    :
        void DoSomething () {}
    };
    void Func (ObjectManager) {}
}

Use fully qualified name:

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.