-
April 19th, 2013, 03:11 AM
#1
Please help me learn STL
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.
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.
for example
Code:
template<typename T>
class MyList:public std::list<T>
{
};
Why is this supposed to be unsafe ?
Thank you
-
April 19th, 2013, 03:50 AM
#2
Re: Please help me learn STL
Originally Posted by terminalXXX
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.
for example
Code:
template<typename T>
class MyList:public std::list<T>
{
};
Why is this supposed to be unsafe ?
Thank you
Code:
int main()
{
std::list<int> *pList = new MyList<int>;
delete pList;
}
The behaviour is undefined, since std::list<int> has no virtual destructor.
Regards,
Paul McKenzie
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|