General discussion

  • Creator
    Topic
  • #2080351

    What is a virtual destructor?

    Locked

    by brrjnessel ·

    Hi,

    Does anybody know why you would use a virtual destructor?

    Thanx,
    John N.

All Comments

  • Author
    Replies
    • #3901194

      What is a virtual destructor?

      by kevin ·

      In reply to What is a virtual destructor?

      The simple answer is that a virtual destructor is necessary if you wish to destroy (using operator delete) a dynamically allocated object (allocated with operator new), using a _pointer to a BASE CLASS of that object_. The virtual destructor ensuresthe correct destructor sequence is called, and the object is cleaned up correctly, even if you are calling delete using a base-class pointer.

      IT IS MY RECOMMENDATION THAT you _ALWAYS_ code your destructors as VIRTUAL if your class contains ANY virtual functions. This will avoid pernicious bugs creeping into your code that can be quite difficult to find.

      It’s hard to gauge your experience level from the question so my apologies if I go over obvious stuff from hereon:

      Here’s a [relatively] simple example (I assume you understand virtual functions):

      Let’s say you’re implementing a simple shape drawing program, and the base class for all shapes is cShape. cShape contains an x,y coordinate and a virtual function called Paint (), which draws the shape. Y

    • #3901094

      What is a virtual destructor?

      by ram_cv ·

      In reply to What is a virtual destructor?

      Dear John,
      Let me just explain this to you with an example :
      Let’s take three classes :
      Base , Derived1, Derived2.

      Derived1 and Derived2 are derived from Base.

      Now suppose Base has a non virtual destructor. Now supposing a pointer to baseis declared and it is currently pointing to a derived1 object. Now we delete this pointer, what will happen is the destructor of Base will be called. But actually OOPS emphasizes that the destructor of Derived1 should be called as currently the pointer is pointing to Derived1’s object. Similarly if the object is pointing to Derived2 then that destructor should be called.
      Now if we had the Base Destructor as Virtual and override it with the Destructors in the Derived classes then on destructingthe Derived object the suitable Destructor will be called.
      Hence whenever you inherit a class a Virtual Destructor is a very good programming practice.
      I hope this helps you. You can also refer to Sams Publications Teach Yourself C++ in 21 Days byJesse Liberty. It is

    • #3900857

      What is a virtual destructor?

      by coop cabo rojo ·

      In reply to What is a virtual destructor?

      Probably this will help you:

      1. Pure virtual destructors are an interesting aspect of C++. If a function is pure virtual, then the class that contains it is abstract,and C++ will not allow it to be instantiated.

      Purity is inherited. Thus:
      struct B {virtual void f() = 0;};
      struct D : B {}; // abstract class, pure virtual f is inherited.

      However, destructors are *not* inherited. Thus:

      struct B {virtual ~B() = 0;};
      struct D : B {} // concrete class. Compiler generates destructor.

      All classes must have a destructor. If one is not specified, then the
      compiler will write one for you. If your destructor is pure virtual,
      you still have to supply an implementation.

      Thus:

      struct B {virtual ~B() = 0;}
      B::~B(){/* do whatever */}

      If you don’t implement the pure virtual destructor, you will get a
      link error.

      ————————————-

      What all this adds up to is this. A pure virtual destructor is exactly like a regular virtual destructor,

    • #3899741

      What is a virtual destructor?

      by lastorck ·

      In reply to What is a virtual destructor?

      Look:
      class Error
      {
      private:
      char* ErrStr;
      public:
      Error(char* str)
      {
      ErrStr = new char(strlen(str)+1);
      strcpy(ErrStr, str);
      }
      virtual ~Error(){delete ErrStr;}
      virtual Put()
      {
      printf(“%s\n”, ErrStr);
      }
      };
      class ErrWithDescr
      {
      private:
      char* Descr;
      public:
      ErrWithDescr(char* str, char* ds )
      : Error(str)
      {
      Descr = new char(strlen(ds)+1);
      strcpy(Descr, ds);
      }
      virtual ~ErrWithDescr(){delete ErrStr; delete Descr;}
      virtual Put()
      {
      printf(“%s : %s\n”, ErrStr, Descr);
      }
      };

      void ShowAndDeleteError(Error* err)
      {
      err->Put();
      delete err;
      }

      void main()
      {
      //…
      Error* e1 = new Error(“Error1”);
      Error* e2 = new ErrWithDescr(“Error2”, “Sm description”);

      ShowAndDeleteError(e1);
      ShowAndDeleteError(e2); //use ~ErrWithDescr();
      }
      It maybe useful for exception objects, objects wich are used in conteiner that own it, … .

    • #3793498

      What is a virtual destructor?

      by brrjnessel ·

      In reply to What is a virtual destructor?

      This question was auto closed due to inactivity

Viewing 4 reply threads