Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Inheritance destructor problem

Reply
Thread Tools

Inheritance destructor problem

 
 
unknown;
Guest
Posts: n/a
 
      11-28-2006
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

 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      11-28-2006
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.

 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      11-28-2006
* 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?
 
Reply With Quote
 
bragbrag@163.com
Guest
Posts: n/a
 
      11-28-2006

"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.

 
Reply With Quote
 
Gavin Deane
Guest
Posts: n/a
 
      11-28-2006

http://www.velocityreviews.com/forums/(E-Mail Removed) 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
newbie -- smart pointer destructor called without destructor everbeing called Jimmy Hartzell C++ 2 05-20-2008 02:20 AM
newbie -- smart pointer destructor called without destructor everbeing called Jimmy Hartzell C++ 0 05-19-2008 07:05 PM
Multiple inheritance constructor/destructor design question Joseph Turian C++ 5 01-15-2007 03:26 AM
compiler generated destructor vs class implemented destructor arun C++ 2 06-13-2006 05:43 AM
Explicit destructor calls from inside base class destructor frs C++ 20 09-21-2005 09:22 AM



Advertisments