Jump to content
  • Advertisement
Sign in to follow this  
Juliean

Vector class: auto convertion from/to float (array?)

This topic is 1962 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

Hello,

 

I wanted to know if/how it was at all possible to emulate such a behaviour:

 

void foo(float x, float y, float z) {};

void goo(Vector3 vector) {};

void main(
{
Vector3 vector; // floating point vector class
foo(vector);
goo(0.0f, 0.0f, 0.0f);
}

 

As you would be able to do with char arrays and the std::string-class? The D3DXVECTOR3-class seems to be able to do this partially, but I'm not sure how to implement such myself. Would save me a lot of times where I'd have two constructors, in case I want to pass values as seperate floats or as a vector... any ideas?

Share this post


Link to post
Share on other sites
Advertisement

No, you cannot implictly convert one parameter into three parameters.

 

Options are to split it out yourself, or to provide a minimal function override that basically says:

 

foo(Vector3 v) { return foo( v.x, v.y, v.z); }

Share this post


Link to post
Share on other sites

What frob said.
You could also play with the preprocessor, but it's rarely a good idea.

#define TOXYZ(v) (v).x,(v).y,(v).z
...
foo(TOXYZ(vector));


Please, don't do the macro thing. It has no advantages over frob's solution and it has several disadvantages. Off the top of my head:
* The debugger will have a hard time telling you what's going on.
* Macros don't follow the scope rules that everything else follows (e.g., you can't put them in a namespace).
* If the expression of type Vector3 being passed is a function call, the macro will result in the function being called three times.

Share this post


Link to post
Share on other sites
struct Vector3
{
	float x, y, z;
};

void foo(Vector3 xyz) {}
void goo(Vector3 vector) {}

int main(int, char**)
{
	Vector3 vector;
	foo(vector);
	goo({0.0f, 0.0f, 0.0f});
}

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!