Marc
Marc

Reputation: 837

How to set the type of a list during runtime?

I have a class which basically contains a list of events (where event is another class)

#include "event.h"
#include <list>

class EventPacket
{

private:
    int max_size;

public:
    std::list<Event> EventList;
    EventPacket();
    ~EventPacket();

    void add(Event *event);
    int size();
    void sort();
    int merge(EventPacket *ep);
    void clear();
};

So far this works fine. But now, I have another class SpecialEvent which is an extension of the Event class. I would like to use the same class EventPacket for putting the special events into a list and I'm wondering what's the correct way of doing this? Is there a way of creating a list without defining its type such that the type (in my case event or specialevent) can be set at runtime?

Upvotes: 0

Views: 139

Answers (5)

Steve
Steve

Reputation: 7271

You must store a list of pointers, rather than a list of objects. For example:

std::list<Event*> EventList;

or

std::list<shared_ptr<Event>> EventList;

You cannot store a list of Event objects because any inherited objects will become sliced. Essentially the inherited parts will be cut off. See: What is object slicing?

Because you are storing pointers, you now have a memory management problem. You must decide whether your EventPacket object should take ownership of the objects and then delete them or whether the calling code should delete them.

To solve this, you may want to look into smart pointers, such as unique_ptr and shared_ptr. The decision you make on ownership semantics will determine whether smart pointers will be useful and if so, which one to choose.

Upvotes: 2

nneonneo
nneonneo

Reputation: 179687

You can make the list a list of Event pointers; these can point to instances of any class derived from Event (including Event itself).

I recommend you use a smart pointer class like unique_ptr or shared_ptr to simplify memory management.

Example:

list<shared_ptr<Event> > EventList;

Upvotes: 1

Some programmer dude
Some programmer dude

Reputation: 409432

Well you pass a pointer to the add function, you can store these pointers in the list instead of copies. Preferably a smart pointer such as std::shared_ptr.

Upvotes: 0

bash.d
bash.d

Reputation: 13217

This is a situation for polymorphism. If you have your class SpecialEvent derived from Event you can use pointers and determine their behavior dynamically at runtime

std::list<Event*> EventList;

If you have virtual methods, the runtime will pick the right one during runtime. Else you might have to use dynamic_cast<> to get the correct type at runtime.

Read this article on polymorphism.

Upvotes: 0

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385325

Read the chapter in your C++ book about polymorphism.

You can't change the list's type at runtime, but if you store pointers in it then you can make use of virtual dispatch to have those pointers point to objects of different levels of inheritance.

Upvotes: 1

Related Questions