Member template functions cannot be virtual - workarounds?

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?

+5
source share
3 answers

- , , , . , any_iterator, - Boost Sandbox/Vault Adobe Labs . Google:

http://thbecker.net/free_software_utilities/type_erasure_for_cpp_iterators/any_iterator.html

+3

, .

, . , , , .

+2

, " ".

STL Container, , . , typedef :

class Entity_Iterator {
public:
    Entity_Iterator operator++() { /* et cetera */ }
    Entity& operator*() const { /* et cetera */ }
private:
    Entity* entity_ptr_;
    // et cetera
}

class Entity_Container {
public:
    typedef Entity_Iterator iterator;
    iterator begin() const { /* et cetera */ }
    iterator end() const  { /* et cetera */ }
private:
    Entity* head_;
    // et cetera
}

STL Container , STL- :

Entity_Container x;
Entity_Container::iterator i = x.begin();
i->GetName();

:

http://www.sgi.com/tech/stl/Container.html

Edit

Google ++ STL, , Google STL.

Edit

, , , Entity, , , ?

, , ?

+1
source

All Articles