Jump to content

  • Log In with Google      Sign In   
  • Create Account


Copy constructor not being called?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 polyfrag   Crossbones+   -  Reputation: 1733

Like
0Likes
Like

Posted 19 June 2013 - 04:07 PM

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];
}


Sponsor:

#2 phil_t   Crossbones+   -  Reputation: 3193

Like
2Likes
Like

Posted 19 June 2013 - 04:15 PM

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, 19 June 2013 - 04:16 PM.


#3 polyfrag   Crossbones+   -  Reputation: 1733

Like
0Likes
Like

Posted 19 June 2013 - 04:38 PM

 

 

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.



#4 polyfrag   Crossbones+   -  Reputation: 1733

Like
0Likes
Like

Posted 19 June 2013 - 04:41 PM

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

 

[edit] Never mind, assignment operator.


Edited by polyfrag, 19 June 2013 - 04:43 PM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS