Can a destructor be called as a normal function?

Discussion in 'C++' started by Asfand Yar Qazi, Aug 25, 2003.

  1. 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/
    Asfand Yar Qazi, Aug 25, 2003
    #1
    1. Advertising

  2. Asfand Yar Qazi

    ES Kim Guest

    "Asfand Yar Qazi" <im_not_giving_it_here@i_hate_spam.com> wrote in message
    news:bic871$7rk$...
    > 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-lite/dtors.html#faq-11.5

    --
    ES Kim
    ES Kim, Aug 25, 2003
    #2
    1. Advertising


  3. > > > 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-lite/dtors.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.
    Newsnet Customer, Aug 25, 2003
    #3
  4. Asfand Yar Qazi

    Chris Theis Guest

    "Newsnet Customer" <> wrote in message
    news:...
    >
    >

    [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
    Chris Theis, Aug 25, 2003
    #4

  5. > [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
    Newsnet Customer, Aug 26, 2003
    #5
  6. Asfand Yar Qazi

    Chris Theis Guest

    "Newsnet Customer" <> wrote in message
    news:...
    >
    >

    [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
    Chris Theis, Aug 26, 2003
    #6
  7. Asfand Yar Qazi

    jeffc Guest

    "Newsnet Customer" <> wrote in message
    news:...
    > >
    > > 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.
    jeffc, Aug 26, 2003
    #7
    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:
    736
    Alf P. Steinbach
    Sep 21, 2005
  2. arun
    Replies:
    2
    Views:
    534
    benben
    Jun 13, 2006
  3. David
    Replies:
    4
    Views:
    363
    Ralph D. Ungermann
    Mar 20, 2007
  4. Jimmy Hartzell
    Replies:
    0
    Views:
    411
    Jimmy Hartzell
    May 19, 2008
  5. Jimmy Hartzell
    Replies:
    2
    Views:
    1,163
    Jimmy Hartzell
    May 20, 2008
Loading...

Share This Page