从任意数量的类派生
我有一个类,我希望其功能依赖于一组插件策略。但是,我不确定如何从任意数量的类派生一个类。
下面的代码是我想要实现的目标的示例。
// insert clever boost or template trickery here
template< class ListOfPolicies >
class CMyClass : public ListOfPolicies
{
public:
CMyClass()
{
// identifiers should be the result of OR-ing all
// of the MY_IDENTIFIERS in the TypeList.
DWORD identifiers;
DoSomeInitialization( ..., identifiers, ... );
}
int MyFunction()
{
return 100;
}
// ...
};
template< class T >
class PolicyA
{
public:
enum { MY_IDENTIFIER = 0x00000001 };
int DoSomethingA()
{
T* pT = static_cast< T* >( this );
return pT->MyFunction() + 1;
};
// ...
};
template< class T >
class PolicyB
{
public:
enum { MY_IDENTIFIER = 0x00000010 };
int DoSomethingB()
{
T* pT = static_cast< T* >( this );
return pT->MyFunction() + 2;
};
// ...
};
int _tmain(int argc, _TCHAR* argv[])
{
CMyClass< PolicyA > A;
assert( A.DoSomethingA() == 101 );
CMyClass< PolicyA, PolicyB > AB
assert( AB.DoSomethingA() == 101 );
assert( AB.DoSomethingB() == 102 );
return 0;
}
谢谢, 保罗·H
I have a class whose functionality I'd like to depend on a set of plug-in policies. But, I'm not sure how to get a class to derive from an arbitrary number of classes.
The code below is an example of what I'm trying to achieve.
// insert clever boost or template trickery here
template< class ListOfPolicies >
class CMyClass : public ListOfPolicies
{
public:
CMyClass()
{
// identifiers should be the result of OR-ing all
// of the MY_IDENTIFIERS in the TypeList.
DWORD identifiers;
DoSomeInitialization( ..., identifiers, ... );
}
int MyFunction()
{
return 100;
}
// ...
};
template< class T >
class PolicyA
{
public:
enum { MY_IDENTIFIER = 0x00000001 };
int DoSomethingA()
{
T* pT = static_cast< T* >( this );
return pT->MyFunction() + 1;
};
// ...
};
template< class T >
class PolicyB
{
public:
enum { MY_IDENTIFIER = 0x00000010 };
int DoSomethingB()
{
T* pT = static_cast< T* >( this );
return pT->MyFunction() + 2;
};
// ...
};
int _tmain(int argc, _TCHAR* argv[])
{
CMyClass< PolicyA > A;
assert( A.DoSomethingA() == 101 );
CMyClass< PolicyA, PolicyB > AB
assert( AB.DoSomethingA() == 101 );
assert( AB.DoSomethingB() == 102 );
return 0;
}
Thanks,
PaulH
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 Boost.MPL 库:
使用作为:
对于“OR-ing all MY_IDENTIFIER”部分,大致如下:
With the Boost.MPL library:
Used as:
For the "OR-ing all MY_IDENTIFIER" part, something along the lines of the following:
为了从任意类型列表派生,您必须列出任意数量的类型。为此,我只知道类型列表。我将在以下代码中使用它:
鉴于此,您可以创建一个从类型列表中的所有类型派生的模板:
我使用以下代码对此进行了测试
VC 和 Comeau 似乎都喜欢此代码,并且它会打印
出来似乎有效。
For deriving from an arbitrary list of types, you'll have to list an arbitrary number of types. For that, I only know type lists. I'll use this for the following code:
Given that, you can create a template that derives from all the types in a type list:
I tested this with the following code
Both VC and Comeau seem to like this code and it prints
so it seems to work.
使用类型列表的可能实现:
A possible implementation using type lists: