April 19th, 2013, 04:11 AM
Please help me learn STL
That was a short quote from someone. But I have a hard time understanding his words and how to write a piece of code as to exemplify what he says.
Everyone knows that you can use STL containers on polymorphic data but since you cannot inherit (safely) from STL types, people assume that you cannot have run-time polymorphic STL objects (ie. Run-time determination of what container type you are passing into an algorithm).
This can easily be achieved through a custom iterator type that contains a polymorphic member. In this way, the exterior iterator type (your custom iterator) passed to the algorithm is static, yet the iterator forwards operations to a polymorphic member whose type is run-time dependent.
Why is this supposed to be unsafe ?
class MyList:public std::list<T>
April 19th, 2013, 04:50 AM
Re: Please help me learn STL
Originally Posted by terminalXXX
The behaviour is undefined, since std::list<int> has no virtual destructor.
std::list<int> *pList = new MyList<int>;
Click Here to Expand Forum to Full Width
This is a Codeguru.com survey!