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