+ Reply to Thread
Results 1 to 2 of 2

Thread: Is there any risk deriving a class from STL container object?

  1. #1

    Is there any risk deriving a class from STL container object?

    Is there any risk deriving a class from STL container object?

    Code:
    class CMyVector : public std::vector<int>
    {
    } ;
    
    void FunVector()
    {
    	CMyVector objMyVector ;
    
    	objMyVector.push_back(10) ;
    	objMyVector.push_back(20) ;
    	objMyVector.push_back(30) ;
    
    	CMyVector::iterator itr = objMyVector.begin() ;
    
    	for (; itr != objMyVector.end(); itr++)
    	{
    		char szMessage[50] ;
    		sprintf(szMessage, "%d\n", *itr) ;
    		OutputDebugString(szMessage) ;
    	}
    
    	objMyVector.clear() ;
    }

    Above code works fine for me.

    I learn that is is not recommended. Why is it so?

  2. #2
    If you are the only user of this class there is no problem.

    But:
    Remember that STL containers do not have virtual destructor which itself recommends that user of STL container should not derive them!

    Note:
    Virtual destructor enables your derive class object to be destructed when created in polymorphic way.

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts