Storing multiple-inheritance objects in container

Viewed 558

A library I use has many types, all of which derive from the same 2 interfaces:

class Huey : public IDuck, public ICartoonCharacter
{
...
};

class Dewey : public IDuck, public ICartoonCharacter
{
...
};

class Louie : public IDuck, public ICartoonCharacter
{
...
};

I'd like to store objects of all the above types in a wrapper class and stick objects of that wrapper class in a container. Of course I should be able to call methods belonging to both interfaces from my wrapper class.

What are my options here? I could think of

  • storing IDuck *s in my wrapper and dynamic_cast-ing to ICartoonCharacter, or
  • using something like boost::any while making my wrapper a class-template, with a couple of static_asserts to ensure the template parameter inherits from IDuck and ICartoonCharacter.

but neither option particularly appeals. Any ideas?

two interfaces, multiple inheritance combine into one container? is a related question, but James Kanze's answer doesn't work for me, as I can't change the 3 classes.

EDIT: Don't use multiple inheritance often, had forgotten syntax. Now inheriting publicly from both interfaces.

EDIT: Now using dynamic_cast instead of static_cast (which won't work).

EDIT: I found both Mike Seymour's and Matthieu M's answers promising. I'll accept one of their answers once I've coded it all up. Thanks!

3 Answers
Related