Sign in to follow this  
polyfrag

Copy constructor not being called?

Recommended Posts

polyfrag    2502

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
phil_t    8084

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
polyfrag    2502

 

 

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
polyfrag    2502

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this