条件成员函数
关于在 C++ 类中条件定义成员函数的建议是什么? (问题集中在限制 DLL 中某些类的外部暴露 - 特别是当这些类作为参数传入时)。显然这不是您想要对数据成员做的事情,但是函数应该没问题,不是吗?
例如:
class A
{
public:
void func1();
#ifdef _CONDITION_
void func2(B b);
#endif
};
编辑: 添加了 public 修饰符以避免示例混淆。
What's the recommendation regarding conditionally defining member functions in a C++ class? (The question is centered around limiting the external exposure of some classes in a DLL - specifically when those classes are passed in as a parameter). Obviously this isn't something you want to do to data members, but functions should be OK shouldn't they?
For example:
class A
{
public:
void func1();
#ifdef _CONDITION_
void func2(B b);
#endif
};
Edited:
Added public modifier to avoid example confusion.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
通常,如果您不想公开导出类的部分内容,那么您应该考虑选择不公开该类,而是提供您的类继承的抽象接口。
例如。
那么您将假设您向 DLL 用户提供 HasStuffIDontWantToExport* 并且它们只有 AbstractExportedInterface 的标头。
编辑:对第一条评论的回应
如果您有某些类型(第三方或其他),您希望 DLL 的客户端能够以某种方式使用,但您不希望它们具有对这些类型的完全访问权限,并且您不这样做可以灵活地使用直接继承层次结构来创建抽象接口。您也许可以使用 pimpl 模式为您希望客户端有限使用的每种类型创建代理接口?
例如。
因此,您可以愉快地导出您喜欢的任何接口,并完全隐藏 DLL 中的实现和第 3 方类型。缺点是您显然必须创建所有这些代理对象接口。
Generally if you wont want to expose parts of an exported class, then you should consider the option of not exposing the class, but instead providing an abstract interface that your class inherits from.
eg.
then you would operate on the assumption that you are providing a HasStuffIDontWantToExport* to the DLL user and they only have headers for AbstractExportedInterface.
EDIT: Response to first comment
If you have some types (3rd party or otherwise) that you want your client of the DLL to be able to use in some way, but you dont want them to have full access to those types, and you dont have the flexibility to use a direct inheritance hierarchy to create an abstract interface. You might be able to use pimpl pattern to create proxy interfaces for each of the types you want your client to have limited usage of?
eg.
So then you can happily export whatever interfaces you like, and completely hide the implementation and 3rd party types inside your DLL. The downside is you obviously then have to create all those proxy object interfaces.
不完全确定您在问什么,但如果成员函数打算对类来说是私有的,请使用“private:”关键字将它们设为私有。
相反,如果它们打算由该类所在模块的上下文中的其他类使用,但您不希望外部实体了解它们,请将它们公开,但从“接口”基类派生该类,并将该接口基类公开给外部实体。
Not entirely sure what you're asking, but if the member functions are intended to be private to the class use the 'private:' keyword to make them private.
If instead they're intended to be used by other classes within the context of the module where the class lives, but you don't want external entities knowing about them, make them public but derive the class from an 'interface' base class, and expose that interface base class to external entities.
这类事情通常是使用
public
/protected
/private
声明以及可能的friend
来完成的,而不是使用预处理器条件。但在我编写的一个程序中,将一组特定函数声明为私有或受保护(因为需要访问它们的独立函数的数量)是一个更大的问题,我在伪私有函数前面加上了前缀带有下划线的名称(以及解释原因的明确注释),以使读者清楚这些函数并不适合一般用途。That sort of thing is generally done using
public
/protected
/private
declarations, and possiblyfriend
s, rather than preprocessor conditions. But in one program I've written where it was more of a problem to declare a certain set of functions as private or protected (because of the number of stand-alone functions that needed access to them), I prefixed the pseudo-private function names with an underscore (along with clear comments explaining why), to make it clear to the reader that those functions weren't meant for general use.您说您想防止某些类的可见性,但您的示例仅隐藏了一个方法。
如果类不构成 DLL 的“公共”接口的一部分,则无需发布标头。例如:
这里“Bar”是实现的一部分,因此不需要发送其标头。如果 Bar 仅由 Foo 使用,您也可以在 Foo 的私有/受保护范围内定义它。
You say you want to prevent visibility of some of the classes, but your example only hides a single method.
If a class doesn't form part of the "public" interface of your DLL you don't need to publish the header. For example:
Here "Bar" is part of the implementation so there is no need to ship its header. If Bar is used only by Foo, you could also define it within the private/protected scope in Foo.