Skip to main content
gamedev.net
🔒 Locked

Returning pointers to classes from functions

Started by hpesoj 10 replies 700+ views
Original Post
hpesoj
hpesoj
I have a function that creates a polygon within my object (opengl, although it doesn't make any difference), and I want to return the pointer to the polygon from the function. It'd be great if someone could tell me how to pass the pointer to the polygon class from the function, here is the relevent code... //Within the polygon class definition ------------ polygonClass addPolygon(XYZStruct vertA, XYZStruct vertB, XYZStruct vertC); //------------------------------------------------ //The function, it doesn't compile at the moment - polygonClass objectClass::addPolygon(XYZStruct vertA, XYZStruct vertB, XYZStruct vertC) { class polygonClass *tmpPoly; tmpPoly = new polygonClass; tmpPoly->setVertexPosition(0, vertA.x, vertA.y, vertA.z); tmpPoly->setVertexPosition(1, vertB.x, vertB.y, vertB.z); tmpPoly->setVertexPosition(2, vertC.x, vertC.y, vertC.z); return &tmpPoly } //------------------------------------------------ Thanks very much for any help.
Joseph Thomson - CLS Productions
C-Junkie
C-Junkie
//CHANGED V
polygonClass* objectClass::addPolygon(XYZStruct vertA, XYZStruct vertB, XYZStruct vertC)
{
class polygonClass *tmpPoly;
tmpPoly = new polygonClass;

tmpPoly->setVertexPosition(0, vertA.x, vertA.y, vertA.z);
tmpPoly->setVertexPosition(1, vertB.x, vertB.y, vertB.z);
tmpPoly->setVertexPosition(2, vertC.x, vertC.y, vertC.z);

return tmpPoly; // CHANGED
}
VolkerG
VolkerG
Either:
polygonClass *objectClass::addPolygon(XYZStruct vertA, XYZStruct vertB, XYZStruct vertC){  polygonClass *tmpPoly;  tmpPoly = new polygonClass;  tmpPoly->setVertexPosition(0, vertA.x, vertA.y, vertA.z);  tmpPoly->setVertexPosition(1, vertB.x, vertB.y, vertB.z);  tmpPoly->setVertexPosition(2, vertC.x, vertC.y, vertC.z);  return tmpPoly;}


or

polygonClass *objectClass::addPolygon(XYZStruct vertA, XYZStruct vertB, XYZStruct vertC){  polygonClass tmpPoly;  tmpPoly.setVertexPosition(0, vertA.x, vertA.y, vertA.z);  tmpPoly.setVertexPosition(1, vertB.x, vertB.y, vertB.z);  tmpPoly.setVertexPosition(2, vertC.x, vertC.y, vertC.z);  return tmpPoly;}

Rember, that the first version creates an object on the heap, but the second version an object on the stack. Disadvantage of the 2nd one, the object will be copied when you pass it around. So I would use version one for larger objects. But you have to delete it, when it is not needed anymore.
hpesoj
hpesoj
Thanks a lot, it compiles now... only it crashes with an error now :P. I'm wondering whether the bit of code inside the class needs to be changed, or whether the call to the function needs to be changed...

//polygon1 is a pointer to the polygonClass
polygon1 = object1->addPolygon(vector1, vector2, vector3);

Thanks.
Joseph Thomson - CLS Productions
hpesoj
hpesoj
I don't see how that second one will work. It doesn't call the constructor function, so the polygon will be useless.
Joseph Thomson - CLS Productions
VolkerG
VolkerG
Why shouldn't the second version call the constructor? The object is constructed on the stack. You only have to ensure that the object will be copied correctly.
C-Junkie
C-Junkie
That second one jsut has problems with it.

VolkerG
VolkerG
Quote:
Original post by C-Junkie
That second one jsut has problems with it.

Version 2 calls the same constructor as version 1 does. So what's the point?
hpesoj
hpesoj
It seems to be a problem with the function calls to setVertexPosition, which is another function within the polygonClass class. vertex[] is an array of pointers of vertexClass. Is there a problem with calling this class function from inside another class function?

void polygonClass::setVertexPosition(int vert, float x, float y, float z){  vertex[vert-1]->position.x = x;  vertex[vert-1]->position.y = y;  vertex[vert-1]->position.z = z;}


EDIT

I found it, I'm using the wrong vertex reference numbers when calling the above function (0-1 = -1 -> vertex[-1] = no! :P).
Joseph Thomson - CLS Productions
C-Junkie
C-Junkie
Quote:
Original post by VolkerG
Quote:
Original post by C-Junkie
That second one jsut has problems with it.

Version 2 calls the same constructor as version 1 does. So what's the point?
It claims to return a pointer, but returns an object instead?
hpesoj
hpesoj
Everything is solved :P (I used the first method), I'll leave you to argue now that I'm happy ;). Thanks for the help everyone.
Joseph Thomson - CLS Productions
VolkerG
VolkerG
Quote:
Original post by C-Junkie
Quote:
Original post by VolkerG
Quote:
Original post by C-Junkie
That second one jsut has problems with it.

Version 2 calls the same constructor as version 1 does. So what's the point?
It claims to return a pointer, but returns an object instead?

Ups, evil copy and paste. Thanks, didn't see the little "*", shouldn't be there.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.