Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Can a destructor be called as a normal function?

Reply
Thread Tools

Can a destructor be called as a normal function?

 
 
Asfand Yar Qazi
Guest
Posts: n/a
 
      08-25-2003
Hi,

Just wondering: if a destructor has some useful stuff in it that I don't
want to copy/paste, can I call it directly as a normal function?
Obviously I'd want it to be called at object deletion as well.

Thanks,
Asfand Yar



--


http://www.it-is-truth.org/

 
Reply With Quote
 
 
 
 
ES Kim
Guest
Posts: n/a
 
      08-25-2003
"Asfand Yar Qazi" <im_not_giving_it_here@i_hate_spam.com> wrote in message
news:bic871$7rk$(E-Mail Removed)...
> Hi,
>
> Just wondering: if a destructor has some useful stuff in it that I don't
> want to copy/paste, can I call it directly as a normal function?
> Obviously I'd want it to be called at object deletion as well.


No.

http://www.parashift.com/c++-faq-lit....html#faq-11.5

--
ES Kim


 
Reply With Quote
 
 
 
 
Newsnet Customer
Guest
Posts: n/a
 
      08-25-2003


> > > Hi,
> > >
> > > Just wondering: if a destructor has some useful stuff in it that I

don't
> > > want to copy/paste, can I call it directly as a normal function?
> > > Obviously I'd want it to be called at object deletion as well.

> >
> > No.
> >
> > http://www.parashift.com/c++-faq-lit....html#faq-11.5
> >
> > --
> > ES Kim
> >

>
> Its good advice but its not quite the answer to the OP's question. You can
> call a destructor as a 'normal function' but you must not have a

destructor
> called twice for the same object.
>
> But why not do this instead? Instead of this
>
> class X
> {
> public:
> ~X() { /* something useful */ }
> ...
> };
>
> write this
>
> class X
> {
> public:
> ~X() { func(); }
> void func() { /* something useful */ }
> ...
> };


That seems redundant to me.


 
Reply With Quote
 
Chris Theis
Guest
Posts: n/a
 
      08-25-2003

"Newsnet Customer" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
>

[SNIP]
>
> That seems redundant to me.
>


Although it might look redundant to you it is not. Think of any situation
where the object needs to perform some kind of reset without being actually
destroyed. To avoid code duplication you'd put this code into a function
which can also be called from the dtor for the cleanup.
A simple situation would be a histogramming class which uses dynamic
allocation for the binning. It stands to good reason to implement a reset
function because you might want to reuse the object with new parameters
without destroying the actual object.

Regards
Chris


 
Reply With Quote
 
Newsnet Customer
Guest
Posts: n/a
 
      08-26-2003


> [SNIP]
> >
> > That seems redundant to me.
> >

>
> Although it might look redundant to you it is not. Think of any situation
> where the object needs to perform some kind of reset without being

actually
> destroyed. To avoid code duplication you'd put this code into a function
> which can also be called from the dtor for the cleanup.
> A simple situation would be a histogramming class which uses dynamic
> allocation for the binning. It stands to good reason to implement a reset
> function because you might want to reuse the object with new parameters
> without destroying the actual object.


Then why not just write a normal reset member function? It would not be
intuitive if the delete operator calls the destructor method to reset the
object. The destructor should be used to give the object 'it's last rite'
before it dies. There are probably reasons for doing it, but I don't see it
in this example.

sdf


 
Reply With Quote
 
Chris Theis
Guest
Posts: n/a
 
      08-26-2003

"Newsnet Customer" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
>

[SNIP]>
> Then why not just write a normal reset member function? It would not be
> intuitive if the delete operator calls the destructor method to reset the
> object. The destructor should be used to give the object 'it's last rite'
> before it dies. There are probably reasons for doing it, but I don't see

it
> in this example.
>


Okay then let's consider the following situation: you're implementing a
simulation using a particle system object. One of the main components of
such a system is mesh generation. Thus the particle object will store a
pointer to a mesh. Of course you might run into the situation where you will
have to recreate the mesh (be it because of user interaction or a
requirement coming up during your simulation). In this case it makes perfect
sense to have a Reset function which will call a delete on the mesh pointer.
The same must be done by the dtor to avoid a memory leak. Hence it stands to
good reason to allow the dtor to call the reset function to avoid code
duplication.

CParticleSystem {

public:
CParticleSystem() : m_pMesh(0) {};
CParticleSystem( CVector2D DimU, CVector2D DimV ) {
m_pMesh = new CMesh( DimU, DimV );
}
~CParticleSystem() { Reset(); };

void CreateMesh( CVector2D DimU, CVector2D DimV ) {

if( m_pMesh)
Reset();

m_pMesh = new CMesh( DimU, DimV );
}

protected:
void Reset() {
// ... insert whatever is needed
delete m_pMesh;
m_pMesh = 0;
}

// ... insert whatever is needed
protected:
CMesh* m_pMesh;
};

Regards
Chris


 
Reply With Quote
 
jeffc
Guest
Posts: n/a
 
      08-26-2003

"Newsnet Customer" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> >
> > write this
> >
> > class X
> > {
> > public:
> > ~X() { func(); }
> > void func() { /* something useful */ }
> > ...
> > };

>
> That seems redundant to me.


It's obviously not redundant if another function wants to call func() also.


 
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
compiler generated destructor vs class implemented destructor arun C++ 2 06-13-2006 05:43 AM
C++ static destructor guaranteed to be called? Paul J. Lucas Java 21 10-22-2005 02:36 AM
Explicit destructor calls from inside base class destructor frs C++ 20 09-21-2005 09:22 AM



Advertisments