[SOURCE DOWNLOAD]
Indispensable Windows Native-C + +: namespaces
Webabcd
Introduced
Essential C + + of Windows Native
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