简体   繁体   English

C++ 如何使函数/变量局部于命名空间?

[英]C++ How do I make a function/variable local to a namespace?

I want to neatly declare functions and variables local to a namespace, so only namespace members can access them.我想整齐地声明一个命名空间的局部函数和变量,所以只有命名空间成员才能访问它们。

In lua, I would use a "local" keyword to do this (in a file/function).在 lua 中,我会使用“本地”关键字来执行此操作(在文件/函数中)。 Is anything related possible in C++?在 C++ 中有什么相关的可能吗?

EDIT编辑

Here's a pseudo-c++ sample of my code:这是我的代码的伪 C++ 示例:

namespace wld {
    const int wldDivis = 4;
    void myFunc(){
        // wldDivis constant used here
    }
    void myFunc2(){
        // wldDivis also used here
    }
}

// Somewhere later
wld::myFunc();
wld::myFunc2();

The variable "wldDivis" is only ever used in namespace wld.变量“wldDivis”只在命名空间 wld 中使用过。 How do I localize/"make it invisible" to outside namespace wld, as a "enforced opacity" if others might screw with the inner workings of my functions?如果其他人可能会干扰我函数的内部工作,我如何将其本地化/“使其不可见”到外部命名空间 wld,作为“强制不透明”? Is a namespace well suited?命名空间是否合适?

IE cannot be accessed outside the namespace ? IE 不能在命名空间之外访问?

Variables declared in a namespace are accessible from the outside using qualifiers (mynamespace::myvariable).在命名空间中声明的变量可以使用限定符 (mynamespace::myvariable) 从外部访问。 This is the way you can access std::cout, for example.例如,这是访问 std::cout 的方式。

Access can be controlled within classes via the public , private and protected keywords.可以通过publicprivateprotected关键字在类内控制访问。

A possible (and clumsy) fix would be to put your "globals" inside a class with private access and then make this class friend with the other classes and functions in the namespace.一个可能(而且笨拙)的解决方法是将您的“全局变量”放在具有私有访问权限的类中,然后使该类与命名空间中的其他类和函数成为朋友 Not only is this a nightmare to maintain, but will also make impossible to access these globals if not from a friend .这不仅是维护的噩梦,而且如果不是从朋友那里访问这些全局变量也将变得不可能。

On the other hand, clever declaration of your functions (so they operate on classes defined in the namespace and perform only related functionality) will instantly solve the problem: everything is accessible because it needs to be so.另一方面,巧妙地声明您的函数(因此它们对命名空间中定义的类进行操作并仅执行相关功能)将立即解决问题:一切都是可访问的,因为它需要如此。

Of course, there are far more experienced coders around so maybe someone can chip in with file level visibility, the "extern" keyword and such (I remember these were a thing in old C).当然,周围有更多经验丰富的编码员,所以也许有人可以利用文件级可见性、“extern”关键字等(我记得这些是旧 C 中的东西)。 In any case, is it possible that you provide us with a sample of your code to have a better view on the subject?.在任何情况下,您是否有可能向我们提供您的代码示例以更好地了解该主题?

AFAIK there is no access restriction in namespaces in C++. AFAIK 在 C++ 的命名空间中没有访问限制。

namespace foo {
    void local_func() {}

    // local_func can be accessed inside the namespace

    void global_func() { local_func(); }
}

// ...

foo::local_func(); // local_func can be accessed outside the namespace

You can achieve something close with static member functions inside a class definition:您可以使用类定义中的静态成员函数来实现一些接近的东西:

class foo {
    private:
        static void local_func() {}

    public:
        // local_func can be accessed inside the "namespace" of the class

        void global_func() { local_func(); }
};

// ...

foo::local_func(); // error : local_func is private to the class

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM