@OP:
You have simplest case of what inheritance is there to solve, you are over complicating it big time (probably because you don't grasp yet how and what inheritance does).
This is exactly what I want, I'll explain: this is a cad projetc where the user can draw entities (lines, polylines, ...). If the user draws 3 lines and 2 polylines (by this order), m_listEntities will have 5 elements. If the user selects some entities (lets say 1 line and 1 polyline) the indexes of the select entities are stored in QList m_listEntities_selected (wich is if of type int, and it will have 2 elements, the indexes of the selected entities).
The user can create a copy of the selected entities and move then with the mouse to a new position. The cicle for is looping throw all the select entities, copies them and add them to m_listEntities wich now will have 7 elements.
This can (and should) be solved using OOP.
You don't need an extra list for the selected entities, all you need is a setSelected() and isSelected() methods in your base entity class.
Using a list, or better yet a set of the selected entities so that you can save the looping when you search you selected items - then use a set that stores the pointers of the selected entities.
But information it self, if an item is selected or not should be encapsulated in to the object, not in an external list!
So:
Instead of you m_listEntities_selected make a:
QSet<BaseClass*> m_setSelectedEnteties;
QSet<BaseClass*> m_setSelectedEnteties;
To copy to clipboard, switch view to plain text mode
There are many ways you can fill this set, one easy way would be that your entities emit a signal when they are selected, and in the slot you just add the address of the sender to the set.
You do the same for removing the items from the set in a slot connected to a "unselected" signal.
Now that you have your selected entities, you can do as the poster above me suggested:
foreach (BaseClass *pSelectedEntity, m_setSelectedEnteties){
BaseClass *pEntity = pSelectedEntity->clone();
m_listEntities->append(pEntity);
}
foreach (BaseClass *pSelectedEntity, m_setSelectedEnteties){
BaseClass *pEntity = pSelectedEntity->clone();
m_listEntities->append(pEntity);
}
To copy to clipboard, switch view to plain text mode
Nice and easy.
As you can see, thanks to correct usage of inheritance, you don't have to bother with types.
The specific implementation for each type is encapsulated in the implementation of the virtual method in the various BaseClass subclasses.
Bookmarks