I understand why member template functions cannot be virtual , but I'm not sure what the best workaround is.
I have code similar to this:
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;
}
};
Obviously, I need dynamic dispatch. But, given that the classes are actually quite large, I donβt want to create a template for the whole class. And I still want to support any iterator.
What is the best way to achieve this?
source
share