Sign in to follow this  
coderWalker

Changing Structs?

Recommended Posts

coderWalker    127
[b]Summary:
[/b]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?

[b]
Explanation:[/b]
How do I convert one struct type to another using my own function?

Basically I want to be able to do the following:
[code]
fVertex3d;
fVertex3d.x = 1.5;
fVertex3d.y= 14.1'
fVertex3d.z = 11.4;

iVertex3d = fVertex3d;
[/code]
Then iVertex3d sshould contain (2,14,11).
Would operator overloading be required for this?

I just don't want to have to type this over and over:
[code]
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)
[/code]
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 ?

[b]Structs and convert function:[/b]
[code]
//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);
}[/code]

Share this post


Link to post
Share on other sites
Khaiy    2148
[quote name='coderWalker' timestamp='1306511455' post='4816462']
I thought OO was just for classes? How would I implement this in a struct?
[/quote]


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 this post


Link to post
Share on other sites
Bregma    9202
[quote name='coderWalker' timestamp='1306511455' post='4816462']
I thought OO was just for classes? How would I implement this in a struct?
[/quote]
Operator overloading is for operators. Operators work on things with type.

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.
[code]
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);
[/code]
The NRVO will elide unnecessary copies, and making the function inline can effectively make most of the code just disappear.

Share this post


Link to post
Share on other sites
Wooh    1088
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 this post


Link to post
Share on other sites
Concentrate    181
Or in your iVertex3d provide an appropriate constructor :
[code]
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);
}
[/code]

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:
[code]
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;
//...
[/code]

Share this post


Link to post
Share on other sites
NightCreature83    5002
[quote name='coderWalker' timestamp='1306508203' post='4816445']
[b]Summary:
[/b]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?

[b]
Explanation:[/b]
How do I convert one struct type to another using my own function?

Basically I want to be able to do the following:
[code]
fVertex3d;
fVertex3d.x = 1.5;
fVertex3d.y= 14.1'
fVertex3d.z = 11.4;

iVertex3d = fVertex3d;
[/code]
Then iVertex3d sshould contain (2,14,11).
Would operator overloading be required for this?

I just don't want to have to type this over and over:
[code]
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)
[/code]
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 ?

[b]Structs and convert function:[/b]
[code]
//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);
}[/code]
[/quote]

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


[code]

//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 */)
}
[/code]


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.

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