条件成员函数

发布于 2024-11-01 07:48:48 字数 280 浏览 1 评论 0原文

关于在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

弥枳 2024-11-08 07:48:48

通常,如果您不想公开导出类的部分内容,那么您应该考虑选择不公开该类,而是提供您的类继承的抽象接口。

例如。

class AbstractExportedInterface
{
public:
    virtual void do_stuff() = 0;
};

class HasStuffIDontWantToExport : public AbstractExportedInterface
{
public:
    void do_stuff();
    void do_other_stuff_that_i_dont_export();
};

那么您将假设您向 DLL 用户提供 HasStuffIDontWantToExport* 并且它们只有 AbstractExportedInterface 的标头。

编辑:对第一条评论的回应

如果您有某些类型(第三方或其他),您希望 DLL 的客户端能够以某种方式使用,但您不希望它们具有对这些类型的完全访问权限,并且您不这样做可以灵活地使用直接继承层次结构来创建抽象接口。您也许可以使用 pimpl 模式为您希望客户端有限使用的每种类型创建代理接口?

例如。

class ExportedAbstractProxyObject
{
public:
    virtual void do_stuff() = 0;
};


#include <3rdPartyType.h>

class ProxyObject
{
public:
    void do_stuff() { pimpl_.actually_do_stuff(); }
private:
    3rdPartyType pimpl_;
};


class ExportedAbstractProxyOtherObject
{
public:
    virtual void do_stuff_with_thing(ExportedAbstractProxyObject* thing) = 0;
};


class ProxyOtherObject
{
public:
    void do_stuff_with_thing(ExportedAbstractProxyObject* thing) { thing->do_stuff(); }
};

因此,您可以愉快地导出您喜欢的任何接口,并完全隐藏 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.

class AbstractExportedInterface
{
public:
    virtual void do_stuff() = 0;
};

class HasStuffIDontWantToExport : public AbstractExportedInterface
{
public:
    void do_stuff();
    void do_other_stuff_that_i_dont_export();
};

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.

class ExportedAbstractProxyObject
{
public:
    virtual void do_stuff() = 0;
};


#include <3rdPartyType.h>

class ProxyObject
{
public:
    void do_stuff() { pimpl_.actually_do_stuff(); }
private:
    3rdPartyType pimpl_;
};


class ExportedAbstractProxyOtherObject
{
public:
    virtual void do_stuff_with_thing(ExportedAbstractProxyObject* thing) = 0;
};


class ProxyOtherObject
{
public:
    void do_stuff_with_thing(ExportedAbstractProxyObject* thing) { thing->do_stuff(); }
};

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.

枕梦 2024-11-08 07:48:48

不完全确定您在问什么,但如果成员函数打算对类来说是私有的,请使用“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.

北城半夏 2024-11-08 07:48:48

这类事情通常是使用 public/protected/private 声明以及可能的 friend 来完成的,而不是使用预处理器条件。但在我编写的一个程序中,将一组特定函数声明为私有或受保护(因为需要访问它们的独立函数的数量)是一个更大的问题,我在伪私有函数前面加上了前缀带有下划线的名称(以及解释原因的明确注释),以使读者清楚这些函数并不适合一般用途。

That sort of thing is generally done using public/protected/private declarations, and possibly friends, 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.

黎歌 2024-11-08 07:48:48

您说您想防止某些类的可见性,但您的示例仅隐藏了一个方法。

如果类不构成 DLL 的“公共”接口的一部分,则无需发布标头。例如:

// foo.h
class Bar;

class Foo
{
    private Bar* _bar;
    ...
}

这里“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:

// foo.h
class Bar;

class Foo
{
    private Bar* _bar;
    ...
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文