• FEATURED

View more

View more

View more

Image of the Day Submit

IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

C++ class question

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

7 replies to this topic

#1Gnark  Members

Posted 04 December 2011 - 12:54 PM

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

glVertex3f(vec.x, vec.y, vec.z);

thanks.

#2rdragon1  Members

Posted 04 December 2011 - 12:58 PM

No, but you can write a function that does that

#3Gnark  Members

Posted 04 December 2011 - 01:03 PM

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?

#4Álvaro  Members

Posted 04 December 2011 - 01:09 PM

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?

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.

#5SiCrane  Moderators

Posted 04 December 2011 - 01:15 PM

Of course, since C++ supports function overloading, you can also call that function glVertex3f().

#6kilah  Members

Posted 04 December 2011 - 01:23 PM

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?

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.

#7Gnark  Members

Posted 04 December 2011 - 01:29 PM

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

#8kilah  Members

Posted 04 December 2011 - 01:37 PM

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.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.