C++ class question

Started by
6 comments, last by kilah 12 years, 4 months ago
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.
Advertisement
No, but you can write a function that does that

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 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.
Of course, since C++ supports function overloading, you can also call that function glVertex3f().

[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.
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 :)
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.

This topic is closed to new replies.

Advertisement