CopyConstructor
Hi,
i have a Question about my CopyConstructor,
anyone knows why that dont work ????
The Program crasged when it''s call the Destructor
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];
m_pMeshes=par.m_pMeshes;
m_numMaterials=par.m_numMaterials;
m_pMaterials=new Material[m_numMaterials];
m_pMaterials=par.m_pMaterials;
m_numTriangles=par.m_numTriangles;
m_pTriangles=new Triangle[m_numTriangles];
m_pTriangles=par.m_pTriangles;
m_numVertices=par.m_numVertices;
m_pVertices= new Vertex[m_numVertices];
m_pVertices=par.m_pVertices;
Texture=par.Texture;
}
CMSModel *Model=new CMSModel();
Here is the Code to Copy
CMSModel *newModel=new CMSModel(*Model);
wolf
You aren''t copying the arrays properly; use memcpy, not the assignment operator. What your code currently does is copy over the pointer, not the array.
How appropriate. You fight like a cow.
How appropriate. You fight like a cow.
There's quite a few new's in there, got all the appropriate deletes?
Edit: Yup, Sneftel is right.
[edited by - Wildfire on August 11, 2003 9:42:20 AM]
Edit: Yup, Sneftel is right.
[edited by - Wildfire on August 11, 2003 9:42:20 AM]
How do I set my laser printer on stun?
The problem is your allocations and assignations lines, like :
m_pMeshes=new Mesh[m_numMeshes];
m_pMeshes=par.m_pMeshes;
I''m sure that in your destructor you call: delete [] m_pMeshes,
and so you''ll delete multiple time the same memory area ...
What you really want to do in your constructor is probably this :
m_pMeshes=new Mesh[m_numMeshes];
for(int m=0; m < m_numMeshes ;++m)
{
m_pMeshes[m] = par.m_pMeshes[m]
}
Do the same thing for Materials, Triangles and vertices

Sphax
I have try this befor
memcpy(m_pVertices, par.m_pVertices, sizeof(Vertex));
but this dosent works too :-(
My Destructor:
.
.
m_numMeshes = 0;
if ( m_pMeshes != NULL )
{
delete[] m_pMeshes;
m_pMeshes = NULL;
}
.
.
.
memcpy(m_pVertices, par.m_pVertices, sizeof(Vertex));
but this dosent works too :-(
My Destructor:
.
.
m_numMeshes = 0;
if ( m_pMeshes != NULL )
{
delete[] m_pMeshes;
m_pMeshes = NULL;
}
.
.
.
quote:
memcpy(m_pVertices, par.m_pVertices, sizeof(Vertex));
shouldn't that be
memcpy(m_pVertices, par.m_pVertices, par.m_numVertices *sizeof(Vertex));
You want to copy all of the vertices, not just a single one?
edit: Checking the pointer for null, and setting the pointer/counter to null/0 in the destructor is pretty much unneeded. As soon the the object is destroyed no one has access to those anymore.
[edited by - Wildfire on August 11, 2003 9:56:14 AM]
How do I set my laser printer on stun?
Ok,
the Problem is here
glBegin( GL_TRIANGLES );
{
for ( int j = 0; j < m_pMeshes.m_numTriangles; j++ )
{
int triangleIndex = m_pMeshes.m_pTriangleIndices[j];<br> <br> const Triangle* pTri = &m_pTriangles[triangleIndex];<br><br> for ( int k = 0; k < 3; k++ )<br> {<br>———>int index = pTri->m_vertexIndices[k];<————-<br> glNormal3fv( pTri->m_vertexNormals[k] );<br> glTexCoord2f( pTri->m_s[k], pTri->m_t[k] );<br> glVertex3fv( m_pVertices[index].m_location );<br> }<br>}<br>}<br>glEnd();<br><br>with the Copyconstructor<br>''index'' have a wrong Value<br><br>:-(( </i>
the Problem is here
glBegin( GL_TRIANGLES );
{
for ( int j = 0; j < m_pMeshes.m_numTriangles; j++ )
{
int triangleIndex = m_pMeshes.m_pTriangleIndices[j];<br> <br> const Triangle* pTri = &m_pTriangles[triangleIndex];<br><br> for ( int k = 0; k < 3; k++ )<br> {<br>———>int index = pTri->m_vertexIndices[k];<————-<br> glNormal3fv( pTri->m_vertexNormals[k] );<br> glTexCoord2f( pTri->m_s[k], pTri->m_t[k] );<br> glVertex3fv( m_pVertices[index].m_location );<br> }<br>}<br>}<br>glEnd();<br><br>with the Copyconstructor<br>''index'' have a wrong Value<br><br>:-(( </i>
did you first assign the memory before calling memcopy? I agree with the rest of them that your problem is here. In the destructor, it doesn''t matter calling delete on NULL it will not crash your program, but calling delete on memory that has already been freed will crash.
If you don''t like memcopy, use the for loop as suggested by Sphax.
If you don''t like memcopy, use the for loop as suggested by Sphax.
Wildfire,
yes your right, but this dosent work
m_pMeshes=new Mesh[m_numMeshes];
memcpy(m_pMeshes, par.m_pMeshes, par._numMeshes*sizeof(Mesh));
yes your right, but this dosent work
m_pMeshes=new Mesh[m_numMeshes];
memcpy(m_pMeshes, par.m_pMeshes, par._numMeshes*sizeof(Mesh));
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement