🔒 Locked
Returning pointers to classes from functions
Advertisement
Original Post
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.
//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
}
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
}
Either:
or
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.
|
or
|
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.
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.
//polygon1 is a pointer to the polygonClass
polygon1 = object1->addPolygon(vector1, vector2, vector3);
Thanks.
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?
EDIT
I found it, I'm using the wrong vertex reference numbers when calling the above function (0-1 = -1 -> vertex[-1] = no! :P).
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).
Quote:
Original post by C-Junkie Quote:It claims to return a pointer, but returns an object instead?
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?
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.
Advertisement