条件成员函数 [英] Conditional Member Functions

查看:221
本文介绍了条件成员函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关在C ++类中有条件地定义成员函数的建议是什么? (问题集中在限制DLL中某些类的外部暴露 - 特别是当这些类作为参数传递时)。显然,这不是你想对数据成员做的,但是函数应该是否应该不是吗?



例如:

  class A 
{
public:
void func1();

#ifdef _CONDITION_
void func2(B b);
#endif
};

编辑:
添加公开修饰符以避免示例混淆。

解决方案

一般来说,如果你不想暴露导出类的一部分,那么你应该考虑不暴露类的选项,而是提供您的类继承的抽象接口。



例如

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

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

那么你将假设你向DLL用户提供HasStuffIDontWantToExport *



如果您有某些类型(第三方或第三方)否则)你希望你的客户端的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。缺点是你显然然后必须创建所有这些代理对象接口。


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.

解决方案

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.

这篇关于条件成员函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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