define "doesn''t work". What line does it crash on?
How appropriate. You fight like a cow.
CopyConstructor
Ok,
here my Code
Copyconstructor
CMSModel(const CMSModel ∥)
{
m_position=par.m_position;
m_timedelta=par.m_timedelta;
m_velocity=par.m_velocity;
m_speed=par.m_speed;
m_numMeshes=par.m_numMeshes;
m_pMeshes=new Mesh[m_numMeshes];
memcpy(m_pMeshes, par.m_pMeshes, par.m_numMeshes*sizeof(Mesh));
m_numMaterials=par.m_numMaterials;
m_pMaterials=new Material[m_numMaterials];
memcpy(m_pMaterials, par.m_pMaterials, par.m_numMaterials*sizeof(Material));
m_numTriangles=par.m_numTriangles;
m_pTriangles=new Triangle[m_numTriangles];
memcpy(m_pTriangles, par.m_pTriangles, par.m_numTriangles*sizeof(Triangle));
m_numVertices=par.m_numVertices;
m_pVertices= new Vertex[m_numVertices];
memcpy(m_pVertices, par.m_pVertices, par.m_numVertices*sizeof(Vertex));
Texture=par.Texture;
}
My assignment operator:
CMSModel& operator=(const CMSModel ∥)
{
if(this==∥)
return *this;
m_position=par.m_position;
m_timedelta=par.m_timedelta;
m_velocity=par.m_velocity;
m_speed=par.m_speed;
m_numMeshes=par.m_numMeshes;
delete[] m_pMeshes;
m_pMeshes=new Mesh[m_numMeshes];
memcpy(m_pMeshes, par.m_pMeshes, par.m_numMeshes*sizeof(Mesh));
m_numMaterials=par.m_numMaterials;
delete[] m_pMaterials;
m_pMaterials=new Material[m_numMaterials];
memcpy(m_pMaterials, par.m_pMaterials, par.m_numMaterials*sizeof(Material));
m_numTriangles=par.m_numTriangles;
delete[] m_pTriangles;
m_pTriangles=new Triangle[m_numTriangles];
memcpy(m_pTriangles, par.m_pTriangles, par.m_numTriangles*sizeof(Triangle));
m_numVertices=par.m_numVertices;
delete[] m_pVertices;
m_pVertices= new Vertex[m_numVertices];
memcpy(m_pVertices, par.m_pVertices, par.m_numVertices*sizeof(Vertex));
Texture=par.Texture;
return *this;
}
the Destructor (works well without Copyconstructor ;-) )
CMSModel::~CMSModel()
{
int i;
for ( i = 0; i < m_numMeshes; i++ )
delete[] m_pMeshes.m_pTriangleIndices;
for ( i = 0; i < m_numMaterials; i++ )
delete[] m_pMaterials.m_pTextureFilename;<br> <br><br> if ( m_pMeshes != NULL )<br> {<br> delete[] m_pMeshes;<br> }<br><br> if ( m_pMaterials != NULL )<br> {<br> delete[] m_pMaterials;<br> }<br><br> if ( m_pTriangles != NULL )<br> {<br> delete[] m_pTriangles;<br> }<br><br> if ( m_pVertices != NULL )<br> {<br> delete[] m_pVertices;<br> }<br>}<br><br>The Program crashed when its call the Destructor for the Copy Object<br><br><br><br> </i>
here my Code
Copyconstructor
CMSModel(const CMSModel ∥)
{
m_position=par.m_position;
m_timedelta=par.m_timedelta;
m_velocity=par.m_velocity;
m_speed=par.m_speed;
m_numMeshes=par.m_numMeshes;
m_pMeshes=new Mesh[m_numMeshes];
memcpy(m_pMeshes, par.m_pMeshes, par.m_numMeshes*sizeof(Mesh));
m_numMaterials=par.m_numMaterials;
m_pMaterials=new Material[m_numMaterials];
memcpy(m_pMaterials, par.m_pMaterials, par.m_numMaterials*sizeof(Material));
m_numTriangles=par.m_numTriangles;
m_pTriangles=new Triangle[m_numTriangles];
memcpy(m_pTriangles, par.m_pTriangles, par.m_numTriangles*sizeof(Triangle));
m_numVertices=par.m_numVertices;
m_pVertices= new Vertex[m_numVertices];
memcpy(m_pVertices, par.m_pVertices, par.m_numVertices*sizeof(Vertex));
Texture=par.Texture;
}
My assignment operator:
CMSModel& operator=(const CMSModel ∥)
{
if(this==∥)
return *this;
m_position=par.m_position;
m_timedelta=par.m_timedelta;
m_velocity=par.m_velocity;
m_speed=par.m_speed;
m_numMeshes=par.m_numMeshes;
delete[] m_pMeshes;
m_pMeshes=new Mesh[m_numMeshes];
memcpy(m_pMeshes, par.m_pMeshes, par.m_numMeshes*sizeof(Mesh));
m_numMaterials=par.m_numMaterials;
delete[] m_pMaterials;
m_pMaterials=new Material[m_numMaterials];
memcpy(m_pMaterials, par.m_pMaterials, par.m_numMaterials*sizeof(Material));
m_numTriangles=par.m_numTriangles;
delete[] m_pTriangles;
m_pTriangles=new Triangle[m_numTriangles];
memcpy(m_pTriangles, par.m_pTriangles, par.m_numTriangles*sizeof(Triangle));
m_numVertices=par.m_numVertices;
delete[] m_pVertices;
m_pVertices= new Vertex[m_numVertices];
memcpy(m_pVertices, par.m_pVertices, par.m_numVertices*sizeof(Vertex));
Texture=par.Texture;
return *this;
}
the Destructor (works well without Copyconstructor ;-) )
CMSModel::~CMSModel()
{
int i;
for ( i = 0; i < m_numMeshes; i++ )
delete[] m_pMeshes.m_pTriangleIndices;
for ( i = 0; i < m_numMaterials; i++ )
delete[] m_pMaterials.m_pTextureFilename;<br> <br><br> if ( m_pMeshes != NULL )<br> {<br> delete[] m_pMeshes;<br> }<br><br> if ( m_pMaterials != NULL )<br> {<br> delete[] m_pMaterials;<br> }<br><br> if ( m_pTriangles != NULL )<br> {<br> delete[] m_pTriangles;<br> }<br><br> if ( m_pVertices != NULL )<br> {<br> delete[] m_pVertices;<br> }<br>}<br><br>The Program crashed when its call the Destructor for the Copy Object<br><br><br><br> </i>
quote:
Original post by wolf_10
The Program crashed when its call the Destructor for the Copy Object
Yes, we know that. On what line in the destructor?
How appropriate. You fight like a cow.
What you are doing in your copy constructor is creating whats called a shallow copy and is exactly what the default copy constructor does. The problem with this is when you start using pointers. Say you have a pointer that points to A. In your copy constructor, you simply tell the new class to point to A aswell, what you need to do is tell it to point to B, but make sure B holds the same data as A.
So, here's a short example: (note, i haven't had time to test it)
In your example though you have an array, so you'd need to go through each element in a for loop.
Hope this helps
[edited by - desertcube on August 11, 2003 11:09:15 AM]
So, here's a short example: (note, i haven't had time to test it)
class MyClass{public: MyClass():itsPointer(0) {} //Constructor set pointers to null MyClass(const MyClass &); //Copy constructor ~MyClass() {delete itsPointer;} //Destructor int GetData() const {return *itsPointer;} void SetData(int newData) {*itsPointer = newData;}private: int * itsPointer;};MyClass::MyClass(const MyClass & rhs){ itsPointer = new int; if (!itsPointer) //Couldn't get any memory return; //Error *itsPointer = *rhs.itsPointer; //Set itsPointer to the VALUE of rhs's itsPointer}
In your example though you have an array, so you'd need to go through each element in a for loop.
Hope this helps
[edited by - desertcube on August 11, 2003 11:09:15 AM]
hold on, after veiwing you recent post, why are you doing this
for ( i = 0; i < m_numMeshes; i++ )
delete[] m_pMeshes.m_pTriangleIndices;
for ( i = 0; i < m_numMeshes; i++ )
delete[] m_pMeshes.m_pTriangleIndices;
Ah, okay. The problem here is that you have arrays of arrays. These sub-arrays must have space allocated for their copies with new and be moved with a nested for-loop, as before.
How appropriate. You fight like a cow.
How appropriate. You fight like a cow.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement