1

I haven't done much polymorphism in c++, and it's been a long time since I done it in other languages, so I'd like some input as to if what I'm trying to achieve is even possible.

I have a class "Entity", and several other 'specialized' sub-classes which inherit Entity. These sub-classes all have completely different methods (not overriding/reimplementing the same one), as they need to perform different functions.

I'm looking for a way to organize all of these "Entities" into a single list. If I were to create a single array of type "Entity" and assign the sub classes to the array, I would no longer be able to call any of the sub-class functions, only the parent "Entity" functions.

The only messy way I can immediately think of is to create an array for each sub-class, which doesn't seem that neat at all when I think of the amount of sub-classes I intend to make, not to mention the amount of memory the application would need to allocate.

Is there any way to cleanly achieve something similar to what I'm trying to do?

1
  • 1
    If you have the list store std::unique_ptr instances then you can store any heap-allocated subclass of Entity in the list. When iterating the list you could use dynamic_cast to filter for the particular objects you are interested in at that moment. Commented Oct 7, 2014 at 4:11

2 Answers 2

2

If your Entity subclasses really do completely different things, then it may be worth looking again at your design.

The best way to solve this problem would be to add a (perhaps pure) virtual method on the Entity base class, something like

struct Entity
{
    virtual void do_stuff() = 0;
    virtual ~Entity() {}
};

struct En1 : public Entity
{
    // En1-specific methods
    void method1();
    void method2();

    // Override
    void do_stuff() override {
         method1();
         method2();
    }
};

You can then create an array holding pointers to Entitys, i.e.

std::vector<std::unique_ptr<Entity>> vec;
vec.emplace_back(new En1);
vec.emplace_back(new En2);
for (auto&& e : vec) {
    e->do_stuff();
}

The alternative would be to try to use dynamic_cast or typeid to work out which subclass you're dealing with at runtime, but this is fragile and hard to expand in future and certainly not recommended.

Sign up to request clarification or add additional context in comments.

Comments

1

Create your object of any derived classes that you wish, and then store there addresses in an array of Entity pointers.

Use the dynamic_cast or static_cast operator to downcast the pointers in the array to the appropriate derived class and use this casted pointer to call your derived class methods.

4 Comments

Would you be able to provide a tiny example of this?
@cdhowie, yes or virtual destructors.
@ThomasMcLeod Not for destruction but for lifetime management. If you store raw pointers in the list you have to remember to delete each element in the list manually.
It turns out all answers here were valid, however there was one problem I did not foresee. I'm indeed using C++, but I'm also using the Qt libraries, and did not account for the other classes my classes would inherit, and as such casting back and fourth was impossible with all these inheritances.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.