CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jun 2012
    Posts
    38

    Question 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

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Please help me learn STL

    Quote Originally Posted by terminalXXX View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured