The*_* do 6 c++ language-features
在Java中,它们具有包访问说明符,这使得该函数只能由来自同一"包"(命名空间)的类使用,我看到它的好处.特别是当模型的设计在起作用时.你认为这样的东西在C++中会有用吗?
谢谢.
是的,这是可以实现的。您可以通过公共、受保护、私有关键字以及源可见性来限制可见性。
声明的可见性是众所周知的。
源可见性(如果您来自 Java)有点不同。
Java:您有一个对象接口/实现的文件。Cpp:您有一个接口文件。实现可能位于接口或一个或多个实现(cpp、cxx)文件中。
如果您在库的公共接口之外使用抽象类/接口/函数,它们实际上是隐藏的并且不可访问(好吧,如果您有特别爱管闲事的用户转储符号,那么这是错误的 - 那么他们可能会重新声明接口,并且链接到符号但是......这显然是他们未定义的领域)。所以你只需要让你喜欢的符号可见即可。最好使用包/库命名约定来避免链接错误 - 将类放置在为库的私有实现保留的命名空间中。简单的。
它在 C++ 中有用吗?它还不错,尽管我个人认为该语言有更高的优先级。
源可见性示例:
/* 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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5495 次 |
| 最近记录: |