# Changing Structs?

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

## Recommended Posts

Summary:
1)How do I copy a struct of one type to a struct of another type using a conversion function?

2) Is there a faster way to set the variables in a struct then typing out each one?

Explanation:

How do I convert one struct type to another using my own function?

Basically I want to be able to do the following:
 fVertex3d; fVertex3d.x = 1.5; fVertex3d.y= 14.1' fVertex3d.z = 11.4; iVertex3d = fVertex3d; 
Then iVertex3d sshould contain (2,14,11).

I just don't want to have to type this over and over:
 fVertex3d pos1; pos1.x = 1.5; pos1.y= 14.1' pos1.z = 11.4; iVertex3d pos2; pos2.x = align(pos1.x); pos2.y = align(pos1.y); pos2.z = align(pos1.z) 
Also it is required that I use my own align function.

About the struct varaibles, is there a faster way to set them then having to type .x,.y,.z ?

Structs and convert function:
 //Structs struct iVertex3d { int x,y,z; }; struct fVertex3d { float x,y,z; }; //Functions signed int align(float f) { return static_cast<signed int>((f < 0) ? floor(f) : f); }

##### Share on other sites
Try operator overloading. Then, you can define a rule for what happens when you do type = type assignments.

##### Share on other sites
I thought OO was just for classes? How would I implement this in a struct?

##### Share on other sites

I thought OO was just for classes? How would I implement this in a struct?

While the degree depends somewhat on the language you're using, classes and structs are extremely similar-- sometimes basically identical. You should be able to overload an operator for a struct the same way as you would for a class.

##### Share on other sites

I thought OO was just for classes? How would I implement this in a struct?

Type of both 'class' and 'struct' can have constructors, which is what is being recommended by others.

I do not recommend using implicit conversions, that way lies madness. Just write a function with a good name, and call it.
 inline iVertex3d from_fVertex3d(const fVertex3d& f) { iVertex3d i = { align(f.x), align(f.y), align(f.z) }; return i; } iVertex3d iv = from_fVertex3d(fv); 
The NRVO will elide unnecessary copies, and making the function inline can effectively make most of the code just disappear.

##### Share on other sites
In C++ there is no difference between classes and structs other than
1) members are private by default in classes and public by default in structs.
2) structs use public inheritance and classes use private inheritance if you don't specify.
Other than that they are exactly the same.

##### Share on other sites
Or in your iVertex3d provide an appropriate constructor :
 class iVertex3d{ private: int coord[3]; // (x,y,z) public: template<typename T> iVertex3d(const T& x, const T& y, const T& z){ coord[0] = round(x); coord[1] = round(y); coord[2] = round(z); } //... }; int main(){ iVertex3d iv( fvertex.x, fvertex.y,fvertex.z); } 

Also some suggestions,

1) Drop the hungarian notation. Your coding style should be consistent, if your consistent with hungarian notation, then your code will be hard to read.
2) Consider a template vector class:
 template<typename T, int N> class VectorN{ private: T coord[N]; public: VectorN(const T*begin, const T* end); //asserts (end - begin) < N //.... }; typedef VectorN<float,3> Vector3f; typedef VectorN<double,3> Vector3d; typedef VectorN<int,3> Vector3i; //... 

##### Share on other sites

Summary:
1)How do I copy a struct of one type to a struct of another type using a conversion function?

2) Is there a faster way to set the variables in a struct then typing out each one?

Explanation:

How do I convert one struct type to another using my own function?

Basically I want to be able to do the following:
 fVertex3d; fVertex3d.x = 1.5; fVertex3d.y= 14.1' fVertex3d.z = 11.4; iVertex3d = fVertex3d; 
Then iVertex3d sshould contain (2,14,11).

I just don't want to have to type this over and over:
 fVertex3d pos1; pos1.x = 1.5; pos1.y= 14.1' pos1.z = 11.4; iVertex3d pos2; pos2.x = align(pos1.x); pos2.y = align(pos1.y); pos2.z = align(pos1.z) 
Also it is required that I use my own align function.

About the struct varaibles, is there a faster way to set them then having to type .x,.y,.z ?

Structs and convert function:
 //Structs struct iVertex3d { int x,y,z; }; struct fVertex3d { float x,y,z; }; //Functions signed int align(float f) { return static_cast<signed int>((f < 0) ? floor(f) : f); }

1. Write a constructor in iVertex3d that takes a "const fVertex3d&" variable and then hide all the conversion stuff within the constructor. Or as mentioned above use a explicit conversion function that again takes the const reference and returns a new object

 //Constructor option struct iVertex3D { iVertex3D(const fVertex3D& value) {//do your stuff} } //Conversion function fVertex3D converte(const iVertex3D& val) { return fVertex3D(/* pass the values of val converted to float to this constructor */) } 

2. Yes there is as long as the struct is a simple POD(Plain old Data) you can use the "{}" to initialise it's variables e.g.:
iVertex3d newVertex = {1, 2, 3};
The above line will set the variables in order of how they are found in the definition of the type.

1. 1
Rutin
24
2. 2
3. 3
JoeJ
18
4. 4
5. 5

• 38
• 23
• 13
• 13
• 17
• ### Forum Statistics

• Total Topics
631712
• Total Posts
3001849
×