Jump to content

  • Log In with Google      Sign In   
  • Create Account


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.

  • You cannot reply to this topic
7 replies to this topic

#1 Gnark   Members   -  Reputation: 122

Like
0Likes
Like

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

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

thanks.

Sponsor:

#2 rdragon1   Crossbones+   -  Reputation: 1198

Like
0Likes
Like

Posted 04 December 2011 - 12:58 PM

No, but you can write a function that does that

#3 Gnark   Members   -  Reputation: 122

Like
0Likes
Like

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   Crossbones+   -  Reputation: 12862

Like
1Likes
Like

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.

#5 SiCrane   Moderators   -  Reputation: 9539

Like
1Likes
Like

Posted 04 December 2011 - 01:15 PM

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

#6 kilah   Members   -  Reputation: 435

Like
1Likes
Like

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.

#7 Gnark   Members   -  Reputation: 122

Like
0Likes
Like

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

#8 kilah   Members   -  Reputation: 435

Like
0Likes
Like

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.



PARTNERS