p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

c_plus_plus_programming thread: (a) delete-ing objects through base class pointers w/o destructors


Message #1 by "Andrew S" <andrewbs42@h...> on Fri, 22 Feb 2002 09:41:09 -0600
I'm working with old C++ that I didn't write.  We're on a Unix machine,

using some version of the DEC C++ compiler.



I have a question about base classes, derived classes, the

interchangeability of pointers to objects of each, and how each is

destroyed if no explicit destructors are defined.



To simplify my situation, I provide an example:



assume we have a home-grown base class Cb, a class Cd1 derived from Cb,

and another class Cd2 derived from d1.  We have virtual member functions

in each of the classes that override each other, but none of the classes

have any explicit destructors defined.



Now, we have this section of code:



Cb *base_ptr = 0;



// Should the original author have put a type-cast in the next statement?

base_ptr = new Cd2;



//

// work with Cd2's over-rides of Cb's virtual member functions...

//



delete base_ptr;

base_ptr = 0;



With no destructors explicitly defined, and none of the base or derived

clases actually using 'new' anywhere, the default destructors should

suffice when the delete is executed.



My question is: Does C++ understand that in this case the base_ptr

actually points to an object of type Cd2, and not the vanila Cb?  Does C++

know to actually delete an object of type Cd2 in this case?  Or is it just

going to destroy an object of type Cb, as the pointer is declared?  If C++

is only delteing a Cb object, won't this lead to memory leaks?



Respectfully,



Andrew Sackett

AndrewBS42@H...







_________________________________________________________________

Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp.



Message #2 by "John Elkins" <john@v...> on Fri, 22 Feb 2002 10:55:31 -0500
You should have virtual destructors.  Otherwise, yes, you may get memory

leaks.



j



John Elkins

Web and Database Technologies.  Storage Systems

Vermont Database Corporation

400 Upper Hollow Hill Road

Stowe VT  05672-4510 USA

802-249-0914;  xxx-xxx-xxxx  (FAX);  xxx-xxx-xxxx  (residence)

john@v... <mailto:john@v...>

www.vermontdatabase.com <http://www.vermontdatabase.com>





> -----Original Message-----

> From: Andrew S [mailto:andrewbs42@h...]

> Sent: Friday, February 22, 2002 10:41 AM

> To: C++_Programming

> Subject: [c_plus_plus_programming] (a) delete-ing objects through base

> class pointers w/o destructors

>

>

> I'm working with old C++ that I didn't write.  We're on a Unix machine,

> using some version of the DEC C++ compiler.

>

> I have a question about base classes, derived classes, the

> interchangeability of pointers to objects of each, and how each is

> destroyed if no explicit destructors are defined.

>

> To simplify my situation, I provide an example:

>

> assume we have a home-grown base class Cb, a class Cd1 derived from Cb,

> and another class Cd2 derived from d1.  We have virtual member functions

> in each of the classes that override each other, but none of the classes

> have any explicit destructors defined.

>

> Now, we have this section of code:

>

> Cb *base_ptr = 0;

>

> // Should the original author have put a type-cast in the next statement?

> base_ptr = new Cd2;

>

> //

> // work with Cd2's over-rides of Cb's virtual member functions...

> //

>

> delete base_ptr;

> base_ptr = 0;

>

> With no destructors explicitly defined, and none of the base or derived

> clases actually using 'new' anywhere, the default destructors should

> suffice when the delete is executed.

>

> My question is: Does C++ understand that in this case the base_ptr

> actually points to an object of type Cd2, and not the vanila Cb?  Does C++

> know to actually delete an object of type Cd2 in this case?  Or is it just

> going to destroy an object of type Cb, as the pointer is declared?  If C++

> is only delteing a Cb object, won't this lead to memory leaks?

>

> Respectfully,

>

> Andrew Sackett

> AndrewBS42@H...

>

>

>

> _________________________________________________________________

> Get your FREE download of MSN Explorer at

http://explorer.msn.com/intl.asp.








$subst('Email.Unsub').




  Return to Index