Indispensable Windows Native-C + +: namespaces

Source: Internet
Author: User

[SOURCE DOWNLOAD]


Indispensable Windows Native-C + +: namespaces



Webabcd


Introduced
Essential C + + of Windows Native

    • Name space



Example
CppNamespace.h

#pragmaOnce#include<string>using namespacestd;//define a namespace, define a class in it, and declare a functionnamespacenativedll{classCppnamespace { Public:        stringDemo ();  Public:        stringDemo2 ();    }; stringDemo3 (); stringDemo4 ();}


CppNamespace.cpp

/** Name Space*/#include"pch.h"#include"CppNamespace.h" using namespaceNativedll;//not specifying a namespace is globalvoidnamespace_demo1 ();voidNamespace_demo2 ();voidNamespace_demo3 ();//implementing functions in the Nativedll namespacestringCppnamespace::D Emo ()//all that's written is string nativedll::cppnamespace::D emo (){    //definition and use of namespacesNamespace_demo1 (); //nesting and use of namespacesNamespace_demo2 (); //definition and use of namespaces without namesNamespace_demo3 (); returnDemo2 () + DEMO3 () +Demo4 ();}//implementing functions in the Nativedll namespacestringNativedll::d Emo3 ()//You must specify a namespace, or it is a global{    return "Demo3";}//implementing functions in the Nativedll namespacenamespacenativedll{stringcppnamespace::D Emo2 () {return "Demo2"; }    stringDemo4 () {return "Demo4"; }}//Define 2 namespacesnamespacens1{stringgetString () {return "ns1"; }}namespacens2{stringgetString () {return "ns2"; }}namespaceNs2//namespaces can be defined more than once{    stringgetString2 () {return "ns2 getString2"; }}//Use of namespacesvoidNamespace_demo1 () {stringresult =""; //call a function under the specified namespaceresult = Ns1::getstring ();//ns1result = Ns2::getstring ();//ns2//introduce the specified namespace    using namespaceNS2;//then ns2 effectiveresult = GetString ();//ns2    using namespacens1;//after that, ns1 and ns2 work together .//result = GetString ();//compilation error, because ambiguous//introduces a specified function that specifies a namespace    usingns1::getstring;//Then if you use the getString () function, it is from the ns1result = GetString ();//ns1//using Ns2::getstring;//compile error, and using Ns1::getstring; conflict.}//to define 1 nested namespacesnamespacensa{stringgetString () {return "NsA"; }    namespaceNsB {stringgetString () {return "NsB"; }    }}voidNamespace_demo2 () {stringresult =""; //use of nested namespacesresult = Nsa::nsb::getstring ();//NsB//you can set aliases for nested namespaces (non-nested namespaces can also set aliases)    namespaceNS =NSA::NSB; Result= Ns::getstring ();//NsB}//define a namespace with no name under the namespace named NsXnamespacensx{//Anonymous Namespaces    namespace    {        stringgetstringanonymous () {return "getstringanonymous"; }    }    //functions within a namespace without a name can be called directly inside    stringgetString () {return "getString ()"+getstringanonymous (); }}voidNamespace_demo3 () {stringresult =""; //functions in anonymous namespaces under the specified namespace can also be called directly externallyresult = Nsx::getstringanonymous ();//getstringanonymousresult = Nsx::getstring ();//getString () getstringanonymous}



Ok
[SOURCE DOWNLOAD]

Indispensable Windows Native-C + +: namespaces

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.