Let’s say you have a type T and subtypes TSub1, TSub2 etc.
Several of these subtypes are initialised with new TSub(...). The resulting pointers are then stored as elements in:
list<T*> tsList;
The same pointers are also used as keys in:
map<T*,V> tsMap;
Now consider an iteration over tsList, with iterator variable tIter.
Here are my questions:
Will
tsMap[*tIter]andtsMap.find(*tIter)both successfully
find the correct associated value?Will
delete *tItersuccessfully free the full memory block
allocated for the relevantTSubeven though STL sees the type as
T?Assume there is a defined method
T::foo()and overrideTSub::foo().Will
(*tIter)->foo()callT::foo()orTSub::foo()?
The context is: each TSub is desired to be instantiated in singleton fashion, yet be stored in a manner allowing iterative consideration as the supertype, with methods overridden in the subclass being called.
I’d be very grateful for an informed explanation. Thank you for your time!
Yes, but tsMap[*tIter] will create a default value if it doesn’t find the key.
If and only if the destructor of T is virtual
It will call T::foo if T::foo is not virtual. Otherwise it will call TSub::foo()