+ Reply to Thread
Results 1 to 2 of 2

Thread: Should a base class should have virtual destructor always?

  1. #1

    Should a base class should have virtual destructor always?

    Should a base class should have virtual destructor always?

    Thanks.

  2. #2
    Senior Member
    Join Date
    Feb 2012
    Posts
    109
    No, it is not necessary that each Base class should have virtual constructor.

    In below case, you should have virtual destructor for the base class.

    Example:

    Code:
    class CBase
    {
    private:
    	char *pBaseName ; 
    public:	
    	CBase() : pBaseName(NULL)
    	{
    		pBaseName = new char[100] ;
    	}
    
    	virtual ~CBase()
    	{
    		delete pBaseName ;
    		pBaseName = NULL ;
    	}
    } ;
    
    class CDerived : public CBase
    {
    private:
    	char *pDerivedName ; 
    public:	
    	CDerived() : CBase(), pDerivedName(NULL)
    	{
    		pDerivedName = new char[100] ;
    	}
    
    	~CDerived()
    	{
    		delete pDerivedName ;
    		pDerivedName = NULL ;
    	}
    } ;
    
    void UseVirtualDestructor()
    {
            CBase *pBase = new CDerived() ;
    	delete pBase ;
    }

+ 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