destructor

This is a discussion on destructor within the C++ Programming forums, part of the General Programming Boards category; the concept of virtual destructors...

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    2

    Question destructor

    the concept of virtual destructors

  2. #2
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    yes, it is a concept.
    Now, what about it?
    Away.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    What is: ensuring that your derived class objects will be properly destroyed?

    To ensure that the correct destructor is called for objects allocated in the free store, you need to employ virtual destructors. The virtual keyword signals to the compiler that the destructor calls through a pointer or a reference parameter should have dynamic binding, and so the destructor will be selected at runtime. That works in spite of the fact that all the destructors have different names--destructors are treated as a special case for this purpose.

  4. #4
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    169
    Here's a simple example of using a virtual destructor. DerivedA destructor will not be called and DerivedB will.
    Code:
    #include <iostream.h>
    
    class BaseA 
    {
    public:
      ~BaseA() { cout << "Called ~BaseA()\n"; }
    };
    
    class DerivedA : public BaseA 
    {
    public:
      ~DerivedA() { cout << "Called ~DerivedA()\n"; }
    };
    
    class BaseB 
    {
    public:
      virtual ~BaseB() { cout << "Called ~BaseB()\n"; }
    };
    
    class DerivedB : public BaseB 
    {
    public:
      ~DerivedB() { cout << "Called ~DerivedB()\n"; }
    };
    
    int main() 
    {
      BaseA* Ap = new DerivedA;
      delete Ap;
    
      BaseB* Bp = new DerivedB;
      delete Bp;
    
      return 0;
    }

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    2
    Originally posted by Scarlet7
    Here's a simple example of using a virtual destructor. DerivedA destructor will not be called and DerivedB will.
    Code:
    #include <iostream.h>
    
    class BaseA 
    {
    public:
      ~BaseA() { cout << "Called ~BaseA()\n"; }
    };
    
    class DerivedA : public BaseA 
    {
    public:
      ~DerivedA() { cout << "Called ~DerivedA()\n"; }
    };
    
    class BaseB 
    {
    public:
      virtual ~BaseB() { cout << "Called ~BaseB()\n"; }
    };
    
    class DerivedB : public BaseB 
    {
    public:
      ~DerivedB() { cout << "Called ~DerivedB()\n"; }
    };
    
    int main() 
    {
      BaseA* Ap = new DerivedA;
      delete Ap;
    
      BaseB* Bp = new DerivedB;
      delete Bp;
    
      return 0;
    }
    thnx for the example.
    can u provide me with a numerical example which
    actually specifies memory leak when virtual destructors are
    not used

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Code:
    #include <iostream>
    using namespace std;
    
    class BaseA 
    {
    public:
    	BaseA(int bA)
    	{
    		m_baseA = bA;
    	}
    	~BaseA() { cout << "Called ~BaseA()\n"; }
    
    private:
    	int m_baseA;
    };
    
    class DerivedA : public BaseA 
    {
    public:
    	DerivedA(int dA, int bA):BaseA(bA)
    	{
    		m_derivedA = dA;
    	}
    	~DerivedA() { cout << "Called ~DerivedA()\n"; }
    
    private:
    	int m_derivedA;
    };
    
    
    int main()
    {
    	BaseA* Ap = new DerivedA(1, 2);
    	delete Ap;
    
    	return 0;
    	
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Destructor being called on SGI hash_map key
    By cunnus88 in forum C++ Programming
    Replies: 4
    Last Post: 02-10-2009, 11:05 PM
  2. exception in the destructor
    By coletek in forum C++ Programming
    Replies: 3
    Last Post: 01-12-2009, 11:01 AM
  3. Replies: 1
    Last Post: 06-10-2008, 08:38 PM
  4. Destructor inaccessible
    By renanmzmendes in forum C++ Programming
    Replies: 5
    Last Post: 02-19-2008, 10:07 AM
  5. destructor question
    By kocika73 in forum C++ Programming
    Replies: 3
    Last Post: 03-10-2006, 10:29 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21