Jump to content
  • Advertisement
Sign in to follow this  
Gnark

C++ class question

This topic is 2569 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 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
Advertisement

No, but you can write a function that does that


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?

Share this post


Link to post
Share on other sites

[quote name='rdragon1' timestamp='1323025131' post='4890449']
No, but you can write a function that does that


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

[quote name='rdragon1' timestamp='1323025131' post='4890449']
No, but you can write a function that does that


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]

What you are looking for there is:

// 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());

But SirCane solution may be more elegant depending on your point of view.

Share this post


Link to post
Share on other sites
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
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
Sign in to follow this  

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!