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 let's 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 Child's 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, I'm just more confused than I was before.