Inheritance destructor problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • unknown;

    Inheritance destructor problem

    hi,

    i've got a problem with destroying an inherited object.

    i've got the following:


    class CBarcode
    {
    CBarcode ()
    {
    // do stuff 1
    }

    ~CBarcode ()
    {
    // destroy stuff 2
    }
    }



    class CBarcodeCode128 : public CBarcode
    {
    CBarcodeCode128 () : CBarcode()
    {
    // do stuff 3
    }

    ~CBarcodeCode12 8 ()
    {
    // destroy stuff 4
    }
    }



    in my main application i do this:

    CBarcode *barcodeClient;
    bool someKindOfBoole an = true;


    if (someKindOfBool ean)
    {
    barcodeClient= new CBarcodeCode128 ();
    }
    else
    {
    // CBarcodeInterle aved2of5 is like CBarcode128 a child class of
    CBarcode
    barcodeClient= new CBarcodeInterle aved2of5();
    }


    this seems to work

    but when i want to destroy object 'barcodeClient' ( delete
    barcodeClient; ) it seems that only the destructor of CBarocode will
    execute but not the destructor of CBarcodeCode128 .

    How do i deal with this.

    Many thanks

  • Rolf Magnus

    #2
    Re: Inheritance destructor problem

    unknown; wrote:
    hi,
    >
    i've got a problem with destroying an inherited object.
    >
    i've got the following:
    >
    >
    class CBarcode
    {
    CBarcode ()
    {
    // do stuff 1
    }
    >
    ~CBarcode ()
    {
    // destroy stuff 2
    }
    }
    ;
    >
    class CBarcodeCode128 : public CBarcode
    {
    CBarcodeCode128 () : CBarcode()
    {
    // do stuff 3
    }
    >
    ~CBarcodeCode12 8 ()
    {
    // destroy stuff 4
    }
    }
    ;
    >
    in my main application i do this:
    >
    CBarcode *barcodeClient;
    bool someKindOfBoole an = true;
    >
    >
    if (someKindOfBool ean)
    {
    barcodeClient= new CBarcodeCode128 ();
    }
    else
    {
    // CBarcodeInterle aved2of5 is like CBarcode128 a child class of
    CBarcode
    barcodeClient= new CBarcodeInterle aved2of5();
    }
    >
    >
    this seems to work
    >
    but when i want to destroy object 'barcodeClient' ( delete
    barcodeClient; ) it seems that only the destructor of CBarocode will
    execute but not the destructor of CBarcodeCode128 .
    >
    How do i deal with this.
    By making the destrutor virtual.

    Comment

    • Alf P. Steinbach

      #3
      Re: Inheritance destructor problem

      * unknown;:
      >
      but when i want to destroy object 'barcodeClient' ( delete
      barcodeClient; ) it seems that only the destructor of CBarocode will
      execute but not the destructor of CBarcodeCode128 .
      >
      How do i deal with this.
      See FAQ item 20.7.

      Hth.,

      - Alf

      --
      A: Because it messes up the order in which people normally read text.
      Q: Why is it such a bad thing?
      A: Top-posting.
      Q: What is the most annoying thing on usenet and in e-mail?

      Comment

      • bragbrag@163.com

        #4
        Re: Inheritance destructor problem


        "unknown; дµÀ£º
        "
        hi,
        >
        i've got a problem with destroying an inherited object.
        >
        i've got the following:
        >
        >
        class CBarcode
        {
        CBarcode ()
        {
        // do stuff 1
        }
        >
        ~CBarcode ()
        {
        // destroy stuff 2
        }
        }
        >
        >
        >
        class CBarcodeCode128 : public CBarcode
        {
        CBarcodeCode128 () : CBarcode()
        {
        // do stuff 3
        }
        >
        ~CBarcodeCode12 8 ()
        {
        // destroy stuff 4
        }
        }
        >
        >
        >
        in my main application i do this:
        >
        CBarcode *barcodeClient;
        bool someKindOfBoole an = true;
        >
        >
        if (someKindOfBool ean)
        {
        barcodeClient= new CBarcodeCode128 ();
        }
        else
        {
        // CBarcodeInterle aved2of5 is like CBarcode128 a child class of
        CBarcode
        barcodeClient= new CBarcodeInterle aved2of5();
        }
        >
        >
        this seems to work
        >
        but when i want to destroy object 'barcodeClient' ( delete
        barcodeClient; ) it seems that only the destructor of CBarocode will
        execute but not the destructor of CBarcodeCode128 .
        >
        How do i deal with this.
        >
        Many thanks

        In my opinion,you can use "CBarcodeCode12 8 *barcodeClient" in stead of
        "CBarcode *barcodeClient" ,because "barcodeCli ent= new
        CBarcodeCode128 ()" may cut something important about the class
        CBarcodeCode128 away.

        Comment

        • Gavin Deane

          #5
          Re: Inheritance destructor problem


          bragbrag@163.co m wrote:
          "unknown; дµÀ£º
          class CBarcode
          {
          <snip>
          }

          class CBarcodeCode128 : public CBarcode
          {
          <snip>
          }



          in my main application i do this:

          CBarcode *barcodeClient;
          bool someKindOfBoole an = true;


          if (someKindOfBool ean)
          {
          barcodeClient= new CBarcodeCode128 ();
          }
          else
          {
          // CBarcodeInterle aved2of5 is like CBarcode128 a child class of
          CBarcode
          barcodeClient= new CBarcodeInterle aved2of5();
          }
          <snip OP's problem of non-virtual destructor>
          In my opinion,you can use "CBarcodeCode12 8 *barcodeClient" in stead of
          "CBarcode *barcodeClient" ,because "barcodeCli ent= new
          CBarcodeCode128 ()" may cut something important about the class
          CBarcodeCode128 away.
          If you're talking about slicing, then no, you've misunderstood
          something. From the point of view of avoiding slicing there is nothing
          wrong with the OP's code.

          CBarcode *barcodeClient;
          barcodeClient= new CBarcodeCode128 ();

          is correct. It is not the same as

          CBarcode barcode;
          CBarcodeCode128 barcode128;
          barcode = barcode128;

          The last statement here is what you might be worrying about. In this
          code snippet, the assignment of a CBarcodeCode128 to a CBarcode slices
          off all the derived class parts of barcode128 and, after the
          assignment, barcode only holds a copy of the base class part of
          barcode128. This is generally not what is wanted. However, this code
          deals with objects. The OP's code deals with pointers and using
          pointers of type Base* that actually point to a Derived object (for
          classes Base and Derived where Derived derives from Base) is precisely
          how you avoid slicing and achieve polymorphism. It works the same way
          for references.

          Gavin Deane

          Comment

          Working...