C + + Namespace---namespace

Source: Internet
Author: User

C + + namespaces

In a C + + application. For example, you might write a function called Func () that also has the same function func () in another available library. This way, the compiler will not be able to tell which func () function you are using.

Therefore, the concept of namespaces is introduced to solve the above problem, which can be used as additional information to distinguish functions, classes, variables, etc. of the same name in different libraries. The context is defined by using a namespace. In essence, a namespace is defined as a scope.

Define namespaces

The definition of a namespace uses the keyword namespace, followed by the name of the namespace, as follows:

To invoke a function or variable with a namespace, precede it with the name of the namespace, as follows:

Name::code; //code can be a variable or function and name is the name of the namespace

eg

#define_crt_secure_no_warnings#include<iostream>namespacespace_test1{typedefintT; intA = +; voidFucvoid) {Std::cout<<"I am space_test1 \ n"; }}namespacespace_test2{typedefDoubleT; intA = -; voidFucvoid) {Std::cout<<"I am space_test2\n"; }}intMain () {//The same function, variable, type, can be used with the different namespacesSpace_test2::t High =1.1; Space_test1::t Age= -;    Space_test2::fuc ();    Space_test1::fuc (); Std::cout<<"high="<< High <<Std::endl; Std::cout<<"age="<< age<<Std::endl; Std::cout<<"space_test1::a="<< space_test1::a <<Std::endl; Std::cout<<"space_test2::a="<< space_test2::a <<Std::endl; System ("Pause"); return 0;}
using Directives

You can use the Using namespace directive so that you can use a namespace without having to precede the name of the namespace. This instruction tells the compiler that subsequent code will use the name in the specified namespace.

Why we do not recommend using namespace ... The practice of:

Namespaces use such direct declarations, destroying the same name functions, variables, and classes that it wants to solve, although it is easier to do this without causing ambiguity, since it is possible to enter fewer characters, but it is more appropriate to use a namespace prefix if the same function, variable, class is not guaranteed.

C + + Namespace---namespace

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.