Meh*_*dad 5 c++ virtual-functions member-functions
我理解为什么 成员模板功能不能是虚拟的,但我不确定最好的解决方法是什么.
我有一些类似的代码:
struct Entity
{
template<typename It>
virtual It GetChildren(It it) { return it; }
};
struct Person : public Entity
{
template<typename It>
virtual It GetChildren(It it) { *it++ = "Joe"; }
};
struct Node : public Entity
{
Node left, right;
const char *GetName() { return "dummy"; }
template<typename It>
virtual It GetChildren(It it)
{
*it++ = left.GetName();
*it++ = right.GetName();
return it;
}
};
Run Code Online (Sandbox Code Playgroud)
显然,我需要动态调度.但鉴于这些类实际上非常大,我不想模拟整个类.我仍然想支持任何类型的迭代器.
实现这一目标的最佳方法是什么?
如果支持任何类型的迭代器是您唯一想要的,您可以使用使用类型擦除的迭代器。我认为any_iteratorBoost Sandbox/Vault 或 Adobe Labs 或其中之一已经实现了。这是谷歌的第一个结果:
http://thbecker.net/free_software_utilities/type_erasure_for_cpp_iterators/any_iterator.html