views:

276

answers:

4

HI! Anyone know how I can make the line "chug(derlist);" in the code below work?

#include <iostream>
#include <list>
using namespace std;

class Base
{
public:
    virtual void chug() { cout << "Base chug\n"; }
};

class Derived : public Base
{
public:
    virtual void chug() { cout << "Derived chug\n"; }
    void foo() { cout << "Derived foo\n"; }
};

void chug(list<Base*>& alist)
{
    for (list<Base*>::iterator i = alist.begin(), z = alist.end(); i != z; ++i)
        (*i)->chug();
}

int main() 
{
    list<Base*> baselist;
    list<Derived*> derlist;

    baselist.push_back(new Base);
    baselist.push_back(new Base);
    derlist.push_back(new Derived);
    derlist.push_back(new Derived);

    chug(baselist);
    // chug(derlist);  // How do I make this work?

    return 0;
}

The reason I need this is basically, I have a container of very complex objects, which I need to pass to certain functions that only care about one or two virtual functions in those complex objects.

I know the short answer is "you can't," I'm really looking for any tricks/idioms that people use to get around this problem.

Thanks in advance.

+1  A: 

Either store them by pointer (boost::shared_ptr is a popular option), or use Boost ptr_containers that store pointers internally, but offer a nice API on the outside (and of course fully automated deletion).

#include <boost/ptr_container/ptr_vector.hpp>

boost::ptr_vector<FooBase> foos;
foos.push_back(new FooDerived(...));
foos[0].memberFunc();

Polymorphic conversions of containers are simply not possible, so always pass a ptr_vector<Base>& and downcast in the function itself.

Tronic
+1 for ptr_container. Truly a great library.
rlbond
A: 

Maybe you can make chug a template function that casts the template parameter to a Base* type, then calls chug on that.

Chris H
A: 

why not make chug a template based function so it can have instances for base and derived types ?

or,for a better solurion you can use std::for_each.

Findekano
+3  A: 

Your question is odd; the subject asks "how do I put items in a container without losing polymorphism" - but that is begging the question; items in containers do not lose polymorphism. You just have a container of the base type and everything works.

From your sample, it looks what you're asking is "how do I convert a container of child pointers to a container of base pointers?" - and the answer to that is, you can't. child pointers are convertible to base pointers, containers of child pointers are not. They are unrelated types. Although, note that a shared_ptr is convertible to shared_ptr, but only because they have extra magic to make that work. The containers have no such magic.

One answer would be to make chug a template function (disclaimer: I'm not on a computer with a compiler, so I haven't tried compiling this):

template<typename C, typename T>
void chug(const C<T>& container)
{
    typedef typename C<T>::iterator iter;
    for(iter i = container.begin(); i < container.end(); ++i)
    {
        (*i)->chug();
    }
}

Then chug can take any container of any type, as long as it's a container of pointers and has a chug method.

Terry Mahaffey
Hi Terry,I reworded my question to be more accurate.I'm going with your idea-- below is the version that compiles template<typename T> void chug(list<T*> i != z; ++i) (*i)->chug(); }And it works. It's not a perfect solution, because it isn't polymorphism, but it works for what I need. I can't think of any other solution that doesn't require the person deriving from Base to write extra code.
Kyle