• Advertisement
Sign in to follow this  

Copy constructor not being called?

This topic is 1676 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a base class BrushSide with a vertex array member m_va that has a copy constructor.

class BrushSide
{
public:
	Plane m_plane;
	VertexArray m_va;
	unsigned int m_texture;
	
	BrushSide(const BrushSide& original);
	BrushSide(){}
	virtual ~BrushSide(){}
	virtual void usetex();
};
class VertexArray
{
public:
	int numverts;
	Vec3f* vertices;
	Vec2f* texcoords;
	Vec3f* normals;

	VertexArray(const VertexArray& original);
	VertexArray()
	{
		numverts = 0;
	}

	~VertexArray()
	{
		free();
	}

	void alloc(int numv);
	void free();
};

VertexArray::VertexArray(const VertexArray& original)
{
	g_log<<"vertex array copy constructor"<<endl;

	alloc(original.numverts);
	memcpy(vertices, original.vertices, sizeof(Vec3f)*numverts);
	memcpy(texcoords, original.texcoords, sizeof(Vec2f)*numverts);
	memcpy(normals, original.normals, sizeof(Vec3f)*numverts);
}

I have a derived class EdBrushSide with a copy constructor that gets called.

class EdBrushSide : public BrushSide
{
public:
	int m_ntris;
	Triangle2* m_tris;
	Plane m_tceq[2];	//tex coord uv equations

	EdBrushSide(const EdBrushSide& original);
	EdBrushSide();
	EdBrushSide(Vec3f normal, Vec3f point);
	~EdBrushSide();
	void makeva();
	void usetex();
};

EdBrushSide::EdBrushSide(const EdBrushSide& original)
{
	g_log<<"edbrushside copy constructor"<<endl;

	m_plane = original.m_plane;
	m_texture = original.m_texture;
	m_va = original.m_va;

	m_ntris = original.m_ntris;
	m_tceq[0] = original.m_tceq[0];
	m_tceq[1] = original.m_tceq[1];
	m_tris = new Triangle2[m_ntris];
	//memcpy(m_tris, original.m_tris, sizeof(Triangle2)*m_ntris);
	for(int i=0; i<m_ntris; i++)
		m_tris[i] = original.m_tris[i];
}

I see "edbrushside copy constructor" in the log but no "vertex array copy constructor". What's wrong?

 

And before I was doing this, which I think is a better way of doing it, but still wasn't working,

BrushSide::BrushSide(const BrushSide& original) : m_va(original.m_va)
{
	m_plane = original.m_plane;
	m_texture = original.m_texture;
}

EdBrushSide::EdBrushSide(const EdBrushSide& original) : BrushSide(original)
{
	g_log<<"edbrushside copy constructor"<<endl;

	m_ntris = original.m_ntris;
	m_tceq[0] = original.m_tceq[0];
	m_tceq[1] = original.m_tceq[1];
	m_tris = new Triangle2[m_ntris];
	//memcpy(m_tris, original.m_tris, sizeof(Triangle2)*m_ntris);
	for(int i=0; i<m_ntris; i++)
		m_tris[i] = original.m_tris[i];
}

Share this post


Link to post
Share on other sites
Advertisement

The copy constructor for VertexArray wouldn't be invoked in the first bit of code you posted; just the assignment operator.

 

In the second bit of code it should though. You said that code "still wasn't working" - what exactly do you mean?

 

btw, your code should probably follow the rule of three.

Edited by phil_t

Share this post


Link to post
Share on other sites

 

 

You said that code "still wasn't working" - what exactly do you mean?

 

Never mind, you solved my problem. I'm debugging a deleted memory reference bug and that was really what wasn't working.

Share this post


Link to post
Share on other sites

For an STL list push_back should I be editting the copy constructor or assignment operator?

 

[edit] Never mind, assignment operator.

Edited by polyfrag

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement