Pure virtual destructor

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?utf-8?Q?David_C=C3=B4me?=

    #1

    Pure virtual destructor

    Hello everybody.
    I have a question.
    When i want use a pure virtual destructor in one of my classe, i must
    give a definition of this destructor like this:

    class A
    {
    //a lot of things
    virtual ~A() =0;
    };

    A::~A()
    {
    //....
    }

    However,i ask myself why ?
    Does anyone have one reponse ?
    Thanks.

    David Côme.
  • Victor Bazarov

    #2
    Re: Pure virtual destructor

    David Côme wrote:
    Hello everybody.
    I have a question.
    When i want use a pure virtual destructor in one of my classe, i must
    give a definition of this destructor like this:
    >
    class A
    {
    //a lot of things
    virtual ~A() =0;
    };
    >
    A::~A()
    {
    //....
    }
    >
    However,i ask myself why ?
    Does anyone have one reponse ?
    If you ever derive from your class A, an instance of it will have
    to be destructed at some point, most likely (unless all you do is
    dynamically allocate the derived classes and never deallocate them
    in your program). If your destructor is pure and not implemented,
    then an attempt to destruct an instance of 'A' will result in
    a call to a pure virtual function, which has undefined behaviour.
    To avoid undefined behaviour you need to provide the implementation
    for A::~A. That's one response I have.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask


    Comment

    • =?utf-8?Q?David_C=C3=B4me?=

      #3
      Re: Pure virtual destructor

      On Mon, 24 Dec 2007 19:00:31 +0100, Victor Bazarov
      <v.Abazarov@com acast.netwrote:
      If you ever derive from your class A, an instance of it will have
      to be destructed at some point, most likely (unless all you do is
      dynamically allocate the derived classes and never deallocate them
      in your program). If your destructor is pure and not implemented,
      then an attempt to destruct an instance of 'A' will result in
      a call to a pure virtual function, which has undefined behaviour.
      To avoid undefined behaviour you need to provide the implementation
      for A::~A. That's one response I have.
      >
      V

      Thanks

      Comment

      • johanatan

        #4
        Re: Pure virtual destructor

        On Dec 24, 1:00 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
        David Côme wrote:
        Hello everybody.
        I have a question.
        When i want use a pure virtual destructor in one of my classe, i must
        give a definition of this destructor like this:
        >
        class A
        {
        //a lot of things
        virtual ~A() =0;
        };
        >
        A::~A()
        {
        //....
        }
        >
        However,i ask myself why ?
        Does anyone have one reponse ?
        >
        If you ever derive from your class A, an instance of it will have
        to be destructed at some point, most likely (unless all you do is
        dynamically allocate the derived classes and never deallocate them
        in your program). If your destructor is pure and not implemented,
        then an attempt to destruct an instance of 'A' will result in
        a call to a pure virtual function, which has undefined behaviour.
        To avoid undefined behaviour you need to provide the implementation
        for A::~A.
        Actually, looks like he already has the implementation defined. He
        simply needs to remove '= 0' from the declaration. So, there's really
        two problems: the first is trying to make the destructor pure virtual,
        and the second is trying to define the destructor in the base class
        after having made it pure virtual!!

        Comment

        • Rolf Magnus

          #5
          Re: Pure virtual destructor

          johanatan wrote:
          So, there's really two problems: the first is trying to make the
          destructor pure virtual, and the second is trying to define the destructor
          in the base class after having made it pure virtual!!
          No, there is no problem. Both are valid. In fact, you _must_ implement a
          pure virtual destructor.

          Comment

          Working...