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

[源码下载]


不可或缺 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
[源码下载]

posted @   webabcd  阅读(1865)  评论(2编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
历史上的今天:
2013-05-27 重新想象 Windows 8 Store Apps (29) - 图片处理
2012-05-27 千呼万唤 HTML 5 系列文章索引
2010-05-27 精进不休 .NET 4.0 (4) - C# 4.0 新特性之命名参数和可选参数, 动态绑定(dynamic), 泛型协变和逆变, CountdownEvent, Barrier
2007-05-27 [翻译]ASP.NET 2.0中的健康监测系统(Health Monitoring)(2) - 通过Email发送监测信息
2007-05-27 乐在其中设计模式(C#) - 访问者模式(Visitor Pattern)
点击右上角即可分享
微信分享提示