STL Lists and base/derived classes
-
I have four or five kinds of objects, all derived from an indexedObject. indexedObject's have an integer id which can be set and they can be compared with <, >, etc to facilitate sorting, pretty simple class. I would like to create one class that wraps a list and adds some other functionality and can hold all these kinds of objects. Problem is if I use a std::list to hold the objects, I lose access to the functions not in indexedObject. I would store pointers but then sorting and looking up the objects would require lots of extra code. Is there a way to use the std::list list to store the objects and cast them back to their derived type as appropriate? I can't use the assignment operator, I get an error. I thought about all kinds of things like memcpy or something but it seems as if I resort to that I am getting pretty far off the reservation. Any suggestions?
-
I have four or five kinds of objects, all derived from an indexedObject. indexedObject's have an integer id which can be set and they can be compared with <, >, etc to facilitate sorting, pretty simple class. I would like to create one class that wraps a list and adds some other functionality and can hold all these kinds of objects. Problem is if I use a std::list to hold the objects, I lose access to the functions not in indexedObject. I would store pointers but then sorting and looking up the objects would require lots of extra code. Is there a way to use the std::list list to store the objects and cast them back to their derived type as appropriate? I can't use the assignment operator, I get an error. I thought about all kinds of things like memcpy or something but it seems as if I resort to that I am getting pretty far off the reservation. Any suggestions?
Is
RTTI
[^] what are you looking for? :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
I have four or five kinds of objects, all derived from an indexedObject. indexedObject's have an integer id which can be set and they can be compared with <, >, etc to facilitate sorting, pretty simple class. I would like to create one class that wraps a list and adds some other functionality and can hold all these kinds of objects. Problem is if I use a std::list to hold the objects, I lose access to the functions not in indexedObject. I would store pointers but then sorting and looking up the objects would require lots of extra code. Is there a way to use the std::list list to store the objects and cast them back to their derived type as appropriate? I can't use the assignment operator, I get an error. I thought about all kinds of things like memcpy or something but it seems as if I resort to that I am getting pretty far off the reservation. Any suggestions?
If i understood you right, what you are doing is a bit dangerous... however, if you are sure about what you are doing, you could try simply get the address of the certain item and convert it to a pointer of the right class and access it thorough that pointer, so what i mean is something like this:
((CIndexedObjectDerivant *)(&list_of_CIndexedObjects[12]))->MyRequiredMethod();
Good luck and marry xmass!
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
-
I have four or five kinds of objects, all derived from an indexedObject. indexedObject's have an integer id which can be set and they can be compared with <, >, etc to facilitate sorting, pretty simple class. I would like to create one class that wraps a list and adds some other functionality and can hold all these kinds of objects. Problem is if I use a std::list to hold the objects, I lose access to the functions not in indexedObject. I would store pointers but then sorting and looking up the objects would require lots of extra code. Is there a way to use the std::list list to store the objects and cast them back to their derived type as appropriate? I can't use the assignment operator, I get an error. I thought about all kinds of things like memcpy or something but it seems as if I resort to that I am getting pretty far off the reservation. Any suggestions?
FWIW, I was able to do the following - std::list _listA; std::list::iterator _iA; std::list> _listB; std::list>::iterator _iB; dObj * p; int i; dObj o; for (i = 5; i > 0; --i) { o.setID(i); // method in base class (iObj) o.setValue(i); // method in derived class (dObj) _listA.push_back(o); _listB.push_back(o); } _listA.sort(); _listB.sort(); _iA = _listA.begin(); _iB = _listB.begin(); i = (*_iB).getValue(); // works i = _iB->getValue(); // works although it's not necessarily defined? p = static_cast *>(&(*_iA)); i = p->getValue(); // works _iA++; p = static_cast *>(&(*_iA)); i = p->getValue(); // WRONG answer. The id associated with iObj was correct (2) but the //member variables associated with the derived object have vanished. return 0; Guess I will just go with sub classing several different kinds of lists.