Example 1
// A simple namespace declaration with variables and functions namespace XoaX { // Variable int iInteger; // Function int GetInteger() { // Variable accessed without scope resolution qualifier return iInteger; } }
Example 2
// Namespaces are open to extension. // In this example, namespace members in declared multiple sections. namespace XoaX { // Variable int iInteger; } namespace XoaX { // Function int GetInteger() { // Variable accessed without scope resolution qualifier return iInteger; } }
Example 3
// Separate interface and implementation namespace XoaX { // Variable int iInteger; // Function prototype int GetInteger(); } // Function implementation int XoaX::GetInteger() { // Variable accessed without scope resolution qualifier return iInteger; }
Example 4
// Unnamed namespaces are assigned names, // which are unique in their translation unit. // The code below is equivalent to // namespace XXX { // int iInteger; // } // using namespace XXX; // where XXX is a unique name assigned by the compiler namespace { int iInteger; }
Example 5
// Renaming a namespace namespace XoaxDotNet { // Variable int iInteger; } // Create an a shorter alias for the namespace namespace XoaX = XoaxDotNet;
Example 6
// Nested namespaces namespace XoaX { namespace NGraphics { // Function declaration void Drawline(int iX1, int iY1, int iX2, int iY2); } } // Function definition void XoaX::Ngraphics::Drawline(int iX1, int iY1, int iX2, int iY2) { // Drawing code }
Example 1
// A simple output example using "std" and the scope resolution operator
#include <iostream>
int main() {
std::cout << "Hello World!" << std::endl;
return 0;
}
Example 2
// Accessing members of our own namespace #include <iostream> namespace XoaX { // Variable int iInteger; // Function int GetInteger() { // Variable accessed without scope resolution qualifier return iInteger; } // Class class CPoint { public: double mdX; double mdY; } } int main() { // Initialize the integer XoaX::iInteger = 2; // Output the value, by calling the function std::cout << XoaX::GetInteger() << std::endl; // Declare an instance of the a class in the namespace XoaX::CPoint qMyPoint = {3.1, 2.7}; std::cout << qMyPoint.mdX << ", " << qMyPoint.mdY << std::endl; return 0; }
Example 1
// A simple output example using "std" and two "using" declarations
#include <iostream>
int main() {
using std::cout;
using std::endl;
cout << "Hello World!" << endl;
return 0;
}
Example 2
// Accessing members of our own namespace with some using declarations #include <iostream> namespace XoaX { // Variable int iInteger; // Function int GetInteger() { // Variable accessed without scope resolution qualifier return iInteger; } // Class class CPoint { public: double mdX; double mdY; } } int main() { // Using declarations using XoaX::GetInteger(); using XoaX::CPoint qMyPoint; // Initialize the integerstill needs qualification XoaX::iInteger = 2; // Output the value, by calling the functionno qualification std::cout << GetInteger() << std::endl; // Declare an instance of the a class in the // namespaceno qualification CPoint qMyPoint = {3.1, 2.7}; std::cout << qMyPoint.mdX << ", " << qMyPoint.mdY << std::endl; return 0; }
Example 1
// A simple output example using "std" and a "using" directive
#include <iostream>
int main() {
using namespace std;
cout << "Hello World!" << endl;
return 0;
}
Example 2
// Accessing members of our own namespace with a using directive #include <iostream> namespace XoaX { // Variable int iInteger; // Function int GetInteger() { // Variable accessed without scope resolution qualifier return iInteger; } // class class CPoint { public: double mdX; double mdY; } } int main() { // Using directive for XoaX using namespace XoaX; // Using directive for stdno qualification need for cout and endl; using namespace std; // Initialize the integerno qualification iInteger = 2; // Output the value, by calling the functionno qualification cout << GetInteger() << endl; // Declare an instance of the a class in the // namespaceno qualification CPoint qMyPoint = {3.1, 2.7}; cout << qMyPoint.mdX << ", " << qMyPoint.mdY << endl; return 0; }
Example 1
// Nested namespaces namespace XoaxDotNet { int iInteger; namespace NGraphics { // Function void Drawline(int iX1, int iY1, int iX2, int iY2) { // Drawing code } } } int main () { // Aliases namespace XoaX = XoaxDotNet; namespace XoaXGraph = XoaxDotNet::NGraphics; // We can use XoaX instead of XoaxDotNet XoaX::iInteger = 34; // We can use the XoaXGraph instead of XoaxDotNet::Drawline XoaXGraph::Drawline(0, 0, 100, 100); return 0; }
Example 2
// Composition of namespaces namespace NMine { int iInt1; } namespace NYours { int iInt2; } namespace NOurs { using namespace NMine; using namespace NYours; int iInt3; } int main () { // Members can be called by using the name of the composition NOurs::iInt1 = 1; NOurs::iInt2 = 2; NOurs::iInt3 = 3; // Some code return 0; }
Example 3
// Selection of namespace members namespace NMine { int iInt1; int iInt2 } namespace NYours { int iInt2; int iInt3; } namespace NOurs { // Select only these members to add to the namespace using NMine::iInt2; using NYours::iInt3; int iInt1; } int main () { // Use iInt1 declared in NOurs NOurs::iInt1 = 1; // Use iInt2 declared in NMine NOurs::iInt2 = 2; // Use iInt3 declared in NYours NOurs::iInt3 = 3; // Some code return 0; }
Example 4
// Composition of namespaces with preference namespace NMine { int iInt1; int iInt2 } namespace NYours { int iInt2; int iInt3; } namespace NOurs { using namespace NMine; using namespace NYours; // Select NYours::iInt2 instead over NMine::iInt2 using NYours::iInt2; int iInt4; } int main () { // Use NMine::iInt1 NOurs::iInt1 = 1; // Use NYours::iInt2 by selected preference NOurs::iInt2 = 2; // Use NYours::iInt3 NOurs::iInt3 = 3; // Use NOurs::iInt4 NOurs::iInt4 = 4; // Some code return 0; }
Example 1
// Nested namespaces namespace XoaX { int iInteger; namespace NGraphics { namespace NTwoDimensional { // Function void Drawline(int iX1, int iY1, int iX2, int iY2) { // Drawing code } } } } int main () { // We can call the function using multiple scope resolution XoaX::NGraphics::NTwoDimensional::Drawline(0, 0, 100, 100); // Or we can create an alias and use that namespace XoaX2D = XoaX::NGraphics::NTwoDimensional; XoaX2D::Drawline(0, 0, 100, 100); return 0; }
Example 1
// Members of unnamed namespaces have internal linkage // This is equivalent to the following line // static int iInteger; namespace { int iInteger; } int main () { iInteger = 1; // Some code return 0; }
© 20072025 XoaX.net LLC. All rights reserved.