Inheritance destructor problem

Discussion in 'C++' started by unknown;, Nov 28, 2006.

  1. unknown;

    unknown; Guest

    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
    }

    ~CBarcodeCode128 ()
    {
    // destroy stuff 4
    }
    }



    in my main application i do this:

    CBarcode *barcodeClient;
    bool someKindOfBoolean = true;


    if (someKindOfBoolean)
    {
    barcodeClient= new CBarcodeCode128();
    }
    else
    {
    // CBarcodeInterleaved2of5 is like CBarcode128 a child class of
    CBarcode
    barcodeClient= new CBarcodeInterleaved2of5();
    }


    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
     
    unknown;, Nov 28, 2006
    #1
    1. Advertising

  2. unknown;

    Rolf Magnus Guest

    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
    > }
    >
    > ~CBarcodeCode128 ()
    > {
    > // destroy stuff 4
    > }
    > }


    ;

    >
    > in my main application i do this:
    >
    > CBarcode *barcodeClient;
    > bool someKindOfBoolean = true;
    >
    >
    > if (someKindOfBoolean)
    > {
    > barcodeClient= new CBarcodeCode128();
    > }
    > else
    > {
    > // CBarcodeInterleaved2of5 is like CBarcode128 a child class of
    > CBarcode
    > barcodeClient= new CBarcodeInterleaved2of5();
    > }
    >
    >
    > 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.
     
    Rolf Magnus, Nov 28, 2006
    #2
    1. Advertising

  3. * 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?
     
    Alf P. Steinbach, Nov 28, 2006
    #3
  4. unknown;

    Guest

    "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
    > }
    >
    > ~CBarcodeCode128 ()
    > {
    > // destroy stuff 4
    > }
    > }
    >
    >
    >
    > in my main application i do this:
    >
    > CBarcode *barcodeClient;
    > bool someKindOfBoolean = true;
    >
    >
    > if (someKindOfBoolean)
    > {
    > barcodeClient= new CBarcodeCode128();
    > }
    > else
    > {
    > // CBarcodeInterleaved2of5 is like CBarcode128 a child class of
    > CBarcode
    > barcodeClient= new CBarcodeInterleaved2of5();
    > }
    >
    >
    > 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 "CBarcodeCode128 *barcodeClient" in stead of
    "CBarcode *barcodeClient",because "barcodeClient= new
    CBarcodeCode128()" may cut something important about the class
    CBarcodeCode128 away.
     
    , Nov 28, 2006
    #4
  5. unknown;

    Gavin Deane Guest

    wrote:
    > "unknown; дµÀ£º


    > > class CBarcode
    > > {

    <snip>
    > > }
    > >
    > > class CBarcodeCode128 : public CBarcode
    > > {

    <snip>
    > > }
    > >
    > >
    > >
    > > in my main application i do this:
    > >
    > > CBarcode *barcodeClient;
    > > bool someKindOfBoolean = true;
    > >
    > >
    > > if (someKindOfBoolean)
    > > {
    > > barcodeClient= new CBarcodeCode128();
    > > }
    > > else
    > > {
    > > // CBarcodeInterleaved2of5 is like CBarcode128 a child class of
    > > CBarcode
    > > barcodeClient= new CBarcodeInterleaved2of5();
    > > }


    <snip OP's problem of non-virtual destructor>

    > In my opinion,you can use "CBarcodeCode128 *barcodeClient" in stead of
    > "CBarcode *barcodeClient",because "barcodeClient= 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
     
    Gavin Deane, Nov 28, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. frs
    Replies:
    20
    Views:
    758
    Alf P. Steinbach
    Sep 21, 2005
  2. arun
    Replies:
    2
    Views:
    547
    benben
    Jun 13, 2006
  3. Joseph Turian
    Replies:
    5
    Views:
    442
    Grizlyk
    Jan 15, 2007
  4. Jimmy Hartzell
    Replies:
    0
    Views:
    421
    Jimmy Hartzell
    May 19, 2008
  5. Jimmy Hartzell
    Replies:
    2
    Views:
    1,171
    Jimmy Hartzell
    May 20, 2008
Loading...

Share This Page