不可或缺 Windows Native (15) - C++: 命名空间

[源码下载]

作者:webabcd

介绍
不可或缺 Windows Native 之 C++

  • 命名空间

示例
CppNamespace.h

#pragma once 

#include <string>

using namespace std;

// 定义一个命名空间,并在其中定义一个类以及声明一个函数
namespace NativeDll
{
    class CppNamespace
    {
    public:
        string Demo();

    public:
        string Demo2();
    };

    string demo3();

    string demo4();
}

CppNamespace.cpp

/*
 * 命名空间
 */

#include "pch.h"
#include "CppNamespace.h" 

using namespace NativeDll;

// 不指定命名空间则是全局的
void namespace_demo1();
void namespace_demo2();
void namespace_demo3();

// 实现 NativeDll 命名空间中的函数
string CppNamespace::Demo() // 写全了就是 string NativeDll::CppNamespace::Demo()
{
    // 命名空间的定义及使用
    namespace_demo1();

    // 命名空间的嵌套及使用
    namespace_demo2();

    // 没有名字的命名空间的定义及使用
    namespace_demo3();

    return Demo2() + demo3() + demo4();
}

// 实现 NativeDll 命名空间中的函数
string NativeDll::demo3() // 必须要指定命名空间,否则就是全局的
{
    return "demo3";
}

// 实现 NativeDll 命名空间中的函数
namespace NativeDll
{
    string CppNamespace::Demo2()
    {
        return "Demo2";
    }

    string demo4()
    {
        return "demo4";
    }
}

// 定义 2 个命名空间
namespace ns1
{
    string getString()
    {
        return "ns1";
    }
}
namespace ns2
{
    string getString()
    {
        return "ns2";
    }
}
namespace ns2 // 命名空间是可以多次定义的
{
    string getString2()
    {
        return "ns2 getString2";
    }
}

// 命名空间的使用
void namespace_demo1()
{
    string result = "";

    // 调用指定命名空间下的函数
    result = ns1::getString(); // ns1
    result = ns2::getString(); // ns2

    // 引入指定的命名空间
    using namespace ns2; // 之后 ns2 有效
    result = getString(); // ns2

    using namespace ns1; // 之后 ns1 和 ns2 同时有效
    // result = getString(); // 编译错误,因为不明确

    // 引入指定命名空间的指定函数
    using ns1::getString; // 之后如果使用 getString() 函数,则其是来自 ns1 下的
    result = getString(); // ns1

    // using ns2::getString; // 编译错误,和 using ns1::getString; 冲突了
}

// 定义 1 个嵌套的命名空间
namespace nsA
{
    string getString()
    {
        return "nsA";
    }

    namespace nsB
    {
        string getString()
        {
            return "nsB";
        }
    }
}

void namespace_demo2()
{
    string result = "";

    // 嵌套命名空间的使用
    result = nsA::nsB::getString(); // nsB

    // 可以为嵌套命名空间设置别名(非嵌套的命名空间也可以设置别名)
    namespace ns = nsA::nsB;
    result = ns::getString(); // nsB
}

// 在名为 nsX 的命名空间下定义一个没有名字的命名空间
namespace nsX
{
    // 匿名命名空间
    namespace
    {
        string getStringAnonymous()
        {
            return "getStringAnonymous";
        }
    }

    // 内部可以直接调用没有名字的命名空间下的函数
    string getString()
    {
        return "getString() " + getStringAnonymous();
    }
}

void namespace_demo3()
{
    string result = "";

    // 外部也可以直接调用指定命名空间下的匿名命名空间中的函数
    result = nsX::getStringAnonymous(); // getStringAnonymous
    result = nsX::getString(); // getString() getStringAnonymous
}

OK
[源码下载]

时间: 2024-08-28 17:50:43

不可或缺 Windows Native (15) - C++: 命名空间的相关文章

不可或缺 Windows Native 系列文章索引

[源码下载] 作者:webabcd 1.不可或缺 Windows Native (1) - C 语言: hello c 介绍不可或缺 Windows Native 之 C 语言 在 Windows Store Apps 中调用 C/C++ hello c 2.不可或缺 Windows Native (2) - C 语言: 常量,变量,基本数据类型 介绍不可或缺 Windows Native 之 C 语言 常量 变量 基本数据类型 3.不可或缺 Windows Native (3) - C 语言:

不可或缺 Windows Native (10) - C 语言: 文件

[源码下载] 作者:webabcd 介绍不可或缺 Windows Native 之 C 语言 文件 示例cFile.h #ifndef _MYHEAD_FILE_ #define _MYHEAD_FILE_ #ifdef __cplusplus extern "C" #endif char *demo_cFile(char *rootPath); #endif cFile.c /* * 文件 * * 从用户角度讲,文件可分为普通文件和设备文件两种 * 1.普通文件是指保存在磁盘或其它外

不可或缺 Windows Native (20) - C++: 友元函数, 友元类

[源码下载] 作者:webabcd 介绍不可或缺 Windows Native 之 C++ 友元函数 友元类 示例演示友元函数, 友元类CppClass4.h #pragma once #include <string> using namespace std; namespace NativeDll { class CppClass4 { public: string Demo(); }; } CppClass4.cpp /* * 友元(friend)函数, 友元类 * * 友元函数: C+

不可或缺 Windows Native (24) - C++: 运算符重载, 自定义类型转换

[源码下载] 作者:webabcd 介绍不可或缺 Windows Native 之 C++ 运算符重载 自定义类型转换 示例CppOperator.h #pragma once #include <string> using namespace std; namespace NativeDll { class CppOperator { public: string Demo(); }; } CppOperator.cpp /* * 运算符重载, 自定义类型转换 */ #include &qu

不可或缺 Windows Native (23) - C++: 虚函数

[源码下载] 作者:webabcd 介绍不可或缺 Windows Native 之 C++ 虚函数 示例1.基类CppHuman.h #pragma once #include <string> using namespace std; namespace NativeDll { class CppHuman { protected: string Name; public: // 我是虚函数 virtual string Show(); // 我是纯虚函数(后面的“=0”只起形式上的作用,用

不可或缺 Windows Native (18) - C++: this 指针, 对象数组, 对象和指针, const 对象, const 指针和指向 const 对象的指针, const 对象的引用

[源码下载] 不可或缺 Windows Native (18) - C++: this 指针, 对象数组, 对象和指针, const 对象,  const 指针和指向 const 对象的指针, const 对象的引用 作者:webabcd 介绍不可或缺 Windows Native 之 C++ this 指针 对象数组 对象和指针 const 对象 const 指针和指向 const 对象的指针 const 对象的引用 示例1.CppEmployee 类CppEmployee.h #pragma

不可或缺 Windows Native (14) - C++: 文件

[源码下载] 作者:webabcd 介绍不可或缺 Windows Native 之 C++ 文件 示例CppIO2.h #pragma once #include <string> using namespace std; namespace NativeDll { class CppIO2 { public: string Demo(string rootPath); }; } CppIO2.cpp /* * 文件 */ #include "pch.h" #include

不可或缺 Windows Native (12) - C++: 引用类型

[源码下载] 作者:webabcd 介绍不可或缺 Windows Native 之 C++ 引用类型 示例CppReference.h #pragma once #include <string> using namespace std; namespace NativeDll { class CppReference { public: string Demo(); }; } CppReference.cpp /* * 引用类型 * * 引用也可以称之为“别名” * * 注: * 1.声明引

不可或缺 Windows Native (19) - C++: 对象的动态创建和释放, 对象的赋值和复制, 静态属性和静态函数, 类模板

[源码下载] 作者:webabcd 介绍不可或缺 Windows Native 之 C++ 对象的动态创建和释放 对象的赋值和复制 静态属性和静态函数 类模板 示例1.CppEmployee 类CppEmployee.h #pragma once #include <string> using namespace std; namespace NativeDll { class CppEmployee { int Number; // 默认为 private private: // 以下都是