Jump to content
  • Advertisement
Sign in to follow this  
Cribs

Not to sure how to do this

This topic is 4219 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 was wondering if there is an easy way to copy one struct to another of the same datatype without having to make a conversion function in the struct. This is just an example, not my real code. The reason i ask is because i have about 30 structs that contain about 10 to 20 variables each so it will take me a good day or two to make the functions. Thanks in advance :)
struct vector_s
{
	float x, y, z;
};

struct plane_s
{
	int ID;
	vector_s Normal;
};

int main()
{
	plane_s *pPlanes = new plane_s;
	plane_s Planes1, Planes2;

	Planes1.ID = 1;
	Planes1.Normal.x = 1;
	Planes1.Normal.y = 0;
	Planes1.Normal.z = 0;

	Planes2.ID = 1;
	Planes2.Normal.x = 1;
	Planes2.Normal.y = 0;
	Planes2.Normal.z = 0;
	
	// Heres the part that is bad
	pPlanes[0] = Planes1;
	pPlanes[1] = Planes2;
	return 1;
}

Share this post


Link to post
Share on other sites
Advertisement
By default, compiler will generate assignment operator and copy constructor for you (if you do not define your own). They will copy all data in your structs just as you need.
Note that if your structs contain pointers those methods will copy value of the pointer. They will not automatically allocate new data and copy arrays. Thus you need to take great care when freeing such data to prevent multiple deletes on one pointer.

Share this post


Link to post
Share on other sites
Well, it compiles yes, but dosnt run. Crashed on startup and the debugger just stops at the line where im making the conversion. The only way iv been able to do it is:

pPlanes[0].ID = Plane1.ID;
pPlanes[0].normal.x = Plane1.normal.x;
pPlanes[0].normal.y = Plane1.normal.y;
pPlanes[0].normal.z = Plane1.normal.z;


i cant just do
pPlane[0] = Plane1;

Just wont work, dunno if i have to do something else or something.

Share this post


Link to post
Share on other sites

int main()
{
// Here you allocate room for A SINGLE plane_s
plane_s *pPlanes = new plane_s;

// snip

// Here you try to stuff TWO plane_s objects where there's only room for one
pPlanes[0] = Planes1;
pPlanes[1] = Planes2;

// ..and here you forget to delete what you new'd
return 1;
}


What you SHOULD do:

#include <vector>

// snip

int main()
{
std::vector<plane_s> planes;

// snip

planes.push_back(plane1);
planes.push_back(plane2);
}


Share this post


Link to post
Share on other sites
As was mentioned above, a default constructor will automatically be provided for you for structs and classes. This is usually sufficient as long as your struct/class doesn't allocate memory or open resources. You can simply assign the vector_s instance to the vector_s portion of the plane_s struct.
{
plane_s Planes1;
vector_s Vector1;

// populate Vector1

Planes1.ID = 1;
Planes1.Normal = Vector1;
}

Scott
www.slimcalcs.com

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!