Reputation: 19
I have a QList
like this
QList<media *> MyList;
Where media
is an abstract class created by me. I use this list to store objects of tree different classes, all tree inherit from media. I have to keep them all in one singular list.
Now I also have a method like:
void copyListOfMedia(QList<media *> listToCopy)
The final result has to be that MyList
contains the pointers of objects that are a COPY of the objects pointed by listToCopy
.
Now my first idea was to do something like this:
void copyListOfMedia(QList<media *> listToCopy)
{
for(int i=0; i<listToCopy.size(); i++)
{
MyList.append(new media(*(listToCopy[i])));
}
}
But it's not correct because it's allocating an object of abstract class.
So what do you think is the best idea to solve this?
Pls, if you find some better solutions to manage the hole thing I wold accept all the suggestions, since I am new using QT and c++ in general.
At the beginning I thought the solution might be to redefine the copy constructor of the class media (it does not contain any pointer, so I've not done it yet couse I didn't thought it would be a necessity). But also I don't think that's the solution couse it's still trying to allocate an object of abstract class.
Another solution might be creating a visitor class to separate the copying process; but at the end all the objects have to be in the same list and I have no idea how to implement this.
Upvotes: 1
Views: 72