samedi 21 juillet 2018

Abstract and Derived combined with std::list

I am trying to wrap my head around how i would go about implementing the following problem:

I have a Parent class called Parent and many different children classes called Child1, Child2, etc. Now what i ultimately want to be able to do is have a std::list or some kind of container to save and access a bunch of different children objects and their derived functions

for example:

//Parent.h

class Parent
{
public:
    Parent();

    virtual void logic() = 0;

    //strings and other members of every Child
}

//Child1.h

class Child1 : public Gate
{
public:
    Child1 ();
    ~Child1 ();
    void logic();
};

//Child1.cpp ...

void Child1::logic()
{ //Do Stuff }

Now lets say i want to have a std::list or whatever container is best for my Abstract class Parents

//main.cpp

std::list <Parent> foo;

how would i go about filling the list up with Children of different types and how would i go about iterating over this list and calling each Childs logic() function?

I read tons of posts about using unique_ptr, using dynamic cast or using a vector instead of a list but at this point im just more confused then i was before

Aucun commentaire:

Enregistrer un commentaire