函数和/或类的包可访问性 [英] Package accessibility for function and/or class

查看:83
本文介绍了函数和/或类的包可访问性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中,它们具有包访问说明符,它使函数只能由来自同一包(命名空间)的类使用,我看到好的东西。特别是当模型的设计在发挥。你认为这样的东西在C ++中可能有用吗?

谢谢。

In Java they have package access specifier which enables the function to be used only by classes from this same "package" (namespace) and I see good things about it. Especially when design of models are in play. Do you think that something like this could be useful in C++?
Thanks.

推荐答案

可以实现。您可以通过公开,受保护的私人关键字以及来源公开程度限制公开程度。

Yes, this can be accomplished. You may restrict visibility by public, protected, private keywords, as well as source visibility.

已公布的公开程度通常可以理解。

Declared visibility is commonly understood.

源可见性(如果你来自Java)有点不同。

Source visibility (if you're coming from Java) is a bit different.

Java:你有一个文件用于对象接口/实现。
Cpp:你有一个接口的文件。实现可能在于接口或一个或多个实现(cpp,cxx)文件。

Java: you have one file for an object interface/implementation. Cpp: you have one file for an interface. the implementation may lie in the interface, or one or more implementation (cpp, cxx) files.

如果使用抽象类/接口/函数,图书馆,他们有效地隐藏和不可访问(嗯,这是假如果你有特殊的nosy用户转储符号 - 然后他们可能会重新声明接口,链接到符号,但是这显然是未定义的领土) 。所以你只需要使你喜欢的符号可见。最好使用包/库命名约定来避免链接错误 - 将类放在为库的私有实现保留的命名空间中。简单。

If you use abstract classes/interfaces/functions apart from the public interface of the library, they are effectively hidden and not accessible (well, this is false if you have particularly nosy users who dump symbols -- then they may redeclare the interface, and link to the symbols but... that's obviously undefined territory for them). So you only need to make the symbols you like visible. It's a good idea to use a package/library naming convention to avoid linking errors - place the classes in a namespace reserved for the library's private implementation. Simple.

它在C ++中很有用...虽然我个人认为这种语言有更高的优先级。

Would it be useful in C++... it's not bad, although I personally think there are higher priorities for the language.

源可见性示例:

/* publicly visible header file */

namespace MON {
class t_button {
protected:
    t_button();
    virtual ~t_button();
public:
    typedef enum { Default = 0, Glowing = 1 } ButtonType;
    /* ... all public virtual methods for this family of buttons - aka the public interface ... */
public:
    t_button* CreateButtonOfType(const ButtonType& type);
};
}

/* implementation file -- not visible to clients */

namespace MON {
namespace Private {
class t_glowing_button : public t_button {
public:
    t_glowing_button();
    virtual ~t_glowing_button();
public:
    /* ... impl ... */
};
}
}

MON::t_button* MON::t_button::CreateButtonOfType(const ButtonType& type) {
    switch (type) {
        case Glowing :
            return new Private::t_glowing_button();

        case Default :
            return new Private::t_glowing_button();

            /* .... */
        default :
            break;
    }

    /* ... */
    return 0;
}

这篇关于函数和/或类的包可访问性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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