Inheritance destructor problem

U

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
 
R

Rolf Magnus

unknown; said:
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.
 
A

Alf P. Steinbach

* 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
 
B

bragbrag

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

Gavin Deane

"unknown; дµÀ£º

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top