具有C ++名称空间的文件范围数据 [英] File scope data with C++ namespaces

查看:71
本文介绍了具有C ++名称空间的文件范围数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些嵌入式C ++代码,当前以非常类似于C的方式编写,我想将其转换为使用名称空间,以实现更好的代码组织.目前,我将我的私有文件作用域函数和变量隐藏在匿名名称空间中,但是我不确定使用此新模式将其隐藏在何处.我是否应该仍然使用匿名名称空间,还是将其添加到.cpp文件中的名称空间,但是头文件不足以阻止外部访问吗?

I've got some embedded C++ code that's currently written in a very C-like way, and I'd like to convert it to use namespaces for better code organization. Currently I hide my private file scope functions and variables in anonymous namespaces, but I'm not sure where to hide it using this new pattern. Should I still use an anonymous namespace, or is adding it to the namespace in the .cpp file, but not the header file sufficient enough to prevent external access?

更具体地说,我有如下代码:

More specifically, I've got code that looks like this:

UI.h

#ifndef UI_H
#define UI_H

//Public data declarations
extern int g_UiPublicVar;

//Public function declarations
void UI_PublicFunc();

#endif

UI.cpp

#include "UI.h"

//Private data and functions
namespace
{
int m_PrivateVar = 10;

void privateFunc()
{
   //Do stuff!
}
}

//Public data definitions
int g_UiPublicVar = 10;

//Public function definitions
void UI_PublicFunc()
{
   m_PrivateVar++;
   privateFunc();
}

...我想将其重组为如下形式:

...and I'd like to restructure it to look like this:

新用户界面.h

#ifndef UI_H
#define UI_H

namespace UI
{

//Public data declarations
extern int publicVar;

//Public function declarations
void publicFunc();

}

#endif

新UI.cpp

#include "UI.h"

namespace UI
{

//Public data definitions
int publicVar = 10;

//Public function definitions
void publicFunc()
{
   m_PrivateVar++;
   privateFunc();
}

}

...我应该在哪里放置m_PrivateVar和privateFunc()?

...where should I put m_PrivateVar and privateFunc()?

推荐答案

解决方案是将其放在私有元素的匿名嵌套名称空间中:

The solution is to put it in an anonymous nested namespace for the private elements:

文件UI.cpp:

namespace UI
{
    namespace  // nested private namespace
    {
        int m_PrivateVar = 10;
        void privateFunc()
        {
            //Do stuff!
        }
    }
    //Public definitions
    ...
}

然后其他编译单元将看不到它,因为匿名名称空间对于每个编译单元都是唯一的.

The other compilation units then can't see it, as the anonymous namespace is unique for each compilation unit.

您可以使用第三个编译单元(包括UI.h)并尝试创建对私有功能的访问来测试此设置:

You can test this setting using a third compilation unit, including UI.h and trying to create access to the private function:

文件main.cpp:

#include "UI.h"
namespace UI {
    extern void privateFunc();   // Hijack temptative
}
int main(int ac, char**av) 
{
    UI::publicFunc();   // yes !! 
    UI::privateFunc();  // compiles, but generates a linking error
                        // private remains private :-)  !!!
}

即使劫机诱惑也将使用匿名名称空间,它也不起作用并且仍然会导致链接错误,因为,正如已经说过的那样,匿名名称空间对于每个编译单元都是唯一的.

Even if the hijack temptative would also use an anonymous namespace, it wouldn't work and still result in linking errors, because, as already said, the anonymous namespace is unique for each compilation unit.

这篇关于具有C ++名称空间的文件范围数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆