Can a destructor be called as a normal function?

A

Asfand Yar Qazi

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
 
N

Newsnet Customer

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

Chris Theis

Newsnet Customer said:
[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
 
N

Newsnet Customer

[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
 
C

Chris Theis

Newsnet Customer said:
[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
 

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

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top