Sign in to follow this  
Gnark

C++ class question

Recommended Posts

Gnark    122
Hello,
I have a class called Vec3 with the members float x,y,z and some functions and operators.

Let's say I have need to use the function glVertex3f(GLfloat x, GLfloat y, GLfloat z);

Is it possible to use:
Vec3 vec = { 0.0f, 0.0f, 0.0f};
glVertex3f(vec);

instead of:
glVertex3f(vec.x, vec.y, vec.z);

thanks.

Share this post


Link to post
Share on other sites
Gnark    122
[quote name='rdragon1' timestamp='1323025131' post='4890449']
No, but you can write a function that does that
[/quote]

I've tried making a function like this:

[code]
float* Vec3::get()
{
float ret[3] = {x,y,z};
return ret;
}
[/code]

but it still counts as one argument. Am I close at all?

Share this post


Link to post
Share on other sites
alvaro    21246
[quote name='Gnark' timestamp='1323025395' post='4890453']
[quote name='rdragon1' timestamp='1323025131' post='4890449']
No, but you can write a function that does that
[/quote]

I've tried making a function like this:
float* Vec3::get()
{
float ret[3] = {x,y,z}
return ret;
}

but it still counts as one argument. Am I close at all?
[/quote]

Returning the address of a local variable won't bring anything good.

What you seem to need is a function that takes a Vec3 and calls glVertex3f with its coordinates.

Share this post


Link to post
Share on other sites
kilah    514
[quote name='Gnark' timestamp='1323025395' post='4890453']
[quote name='rdragon1' timestamp='1323025131' post='4890449']
No, but you can write a function that does that
[/quote]

I've tried making a function like this:

[code]
float* Vec3::get()
{
float ret[3] = {x,y,z};
return ret;
}
[/code]

but it still counts as one argument. Am I close at all?
[/quote]

What you are looking for there is:
[code]
// x,y and z must be contiguous in memory. In short: have them all three together in your class.
float* Vec3::get()
{
return &this->x;
}

glVertex3fv(vec.get());
[/code]
But SirCane solution may be more elegant depending on your point of view.

Share this post


Link to post
Share on other sites
Gnark    122
Ok, thanks for the replies.

Just to be sure, there is no way to return multiple values from one function, so that they are read as multiple arguments in another function?
and if so, how?

Thanks again

Edit: didn't see your post there kilah. I'll try that :)

Share this post


Link to post
Share on other sites
kilah    514
The underneath problem lies on "local variables". You must ensure that your variable lifetime is guaranteed after your function X has returned, so glVertex3fv input is correct. Therefore you must ensure the variable lifetime is bigger than what your local stack funciton offers. The mean and way you ensure that... is your decision. You could instance an array, you can pass a memory address of your data, etc.

Cheers.

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