命名空间中的私有/公共类问题
这是一个关于将类定义为公共类或私有类的作用的问题。
现在,我在命名空间内定义了各种类,我只希望这些类中的一些对外界可见/可用。
因此,例如,如果程序中只有下面的类,我希望 main.cpp 只能看到/使用 MyPublic 类,而不是 MyPrivate 类。 我认为将 MyPrivate 类定义为 private 并将 MyPublic 类定义为 public 可以实现此目的,但下面的代码有效并且 main.cpp 能够声明 MyPrivate 对象。
在 C++ 中可以做到这一点吗?
MyPrivate.h:
namespace MyNamespace{
// only classes inside of the MyNamespace should be able
// to use this
private ref class MyPrivate{
...
};
}
MyPublic.h:
#include "MyPrivate.h"
namespace MyNamespace {
// anyone can declare this
public ref class MyPublic{
...
private:
MyNamespace::MyPrivate^ p;
...
};
}
Main.cpp:
#include "MyPublic.h"
int main(){
MyNamespace::MyPublic p_yes; // this is fine
MyNamespace::MyPrivate p_no; // don't want this to be possible
return 0;
}
This is a question about what defining a class as public or private does.
Right now, I have various classes defined inside of a namespace and I only want some of those classes to be visible/usable to the outside world.
So, for example, if the classes below were the only ones in the program, I would want main.cpp to only be able to see/use the MyPublic class, not the MyPrivate class. I thought that defining the MyPrivate class as private and the MyPublic class as public would accomplish this, but the below code works and main.cpp is able to declare a MyPrivate object.
Is it possible to do this in C++?
MyPrivate.h:
namespace MyNamespace{
// only classes inside of the MyNamespace should be able
// to use this
private ref class MyPrivate{
...
};
}
MyPublic.h:
#include "MyPrivate.h"
namespace MyNamespace {
// anyone can declare this
public ref class MyPublic{
...
private:
MyNamespace::MyPrivate^ p;
...
};
}
Main.cpp:
#include "MyPublic.h"
int main(){
MyNamespace::MyPublic p_yes; // this is fine
MyNamespace::MyPrivate p_no; // don't want this to be possible
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在这种情况下,私有/公共将影响类在程序集外部的可见方式,如果您想创建一个“私有”类,意味着它只能由其他类使用,您可以使用嵌套类机制,例如这:
//编辑:
顺便说一句,您仍然可以将这个嵌套类扔到
public:
部分并像这样使用它:MyNamespace::MyPublic::MyPrivate priv;
private/public in this situation will affect how classes are visible outside an assembly, if you want to create a class that is "private" in the meaning that it can be used only by some other class, you can use nested clas mechanism, like this:
//Edit:
You can by the way still throw this nested class in
public:
section and use it like this:MyNamespace::MyPublic::MyPrivate priv;
private 关键字的含义与您想象的不同。 我将 ref 类的可见性限制在程序集之外。 由于您的 Main() 方法位于同一程序集中,因此引用类型名称没有问题。 请注意,C# 语言的“internal”关键字含义相同。
我假设您确实希望有一天将这些类放在单独的程序集中。 因此,使用 private 确实足够了。 使用嵌套私有类可能会使同一程序集中的代码无法访问该类。
The private keyword means something else than you think. I limits visibility of the ref class beyond the assembly. Since your Main() method is in the same assembly, it has no trouble referencing the type name. Note that the C# language's "internal" keyword means the same thing.
I assume that you really intend for these classes to be in a separate assembly someday. As such, using private is certainly good enough. Using nested private classes can make a class inaccessible to code in the same assembly.
您的公共标头不应包含专用标头。 仅在 MyPublic.cpp 中转发声明私有类并包含标头。 或者如果你使用普通的 C++,我会这么说。 糟糕的 .Net 方言可能会改变一切。
You public header shouldn't include private header. Forward declare private class and include header only in MyPublic.cpp. Or that's what I'd say if you used normal C++. Bastardized .Net dialect might change things.
不幸的是,类上的访问修饰符仅影响您正在构建的程序集外部的可见性。 C++ 不支持以您所描述的方式应用于命名空间的任何类型的访问修饰符。
模拟这种情况的常见习惯是将“私有”代码放入
detail
命名空间中(例如将其放入MyNamespace::detail
中)。 这在例如 boost 库中被大量使用。 按照约定,详细命名空间中的代码只能由封闭命名空间中的代码使用(因此MyNamespace::detail
只能由MyNamespace
中的代码使用),尽管编译器不会为你强制执行此操作。Unfortunately, the access modifiers on a class only affect visibility outside the assembly you're building. C++ doesn't support any sort of access modifiers that apply to namespaces in the way you're describing.
A common idiom for simulating this is to put the "private" code into a
detail
namespace (e.g. put it inMyNamespace::detail
). This is used a lot in e.g. the boost libraries. By convention, code in a detail namespace should only be used by code in the enclosing namespace (soMyNamespace::detail
should only be used by code inMyNamespace
), although the compiler won't enforce this for you.