Jump to content
  • Advertisement
Sign in to follow this  
R4v3nh34rt

How to access classes properly?

This topic is 2643 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Basically, I'm making an engine with the following classes:
Point
Line
Rectangle
Solid (parent)
- Cube (subclass)
Entity


Now, it works like this:
Entity->Cube->Rectangle->Line->Point
a solid needs faces that are made of rectangles, which are made of lines, which are made of two points.

This seemed as a fairly nice way to handle things as I can easily handle , change, manage, edit, set every part of a cube.
Rotation, motion etc are all based on points and every function goes up to the solids. So every point "influences" what is going on in their upper level.
I got a problem with this approach though:
1) is this a nice / convenient / efficient way to do it?
2) if not, what would be a better way to handle things given my necessity of handling each aspect of the solid independently ?
3)
I made a constructor for entity and every other class that look like this:

Entity::Entity(GLfloat size)
{
body = new CubeSolid(1.0,false,false);
spawned = false;
}

CubeSolid::CubeSolid(GLfloat size,bool hidden, bool wireMode) : PrimitiveSolid()
{

faces[0] = new Rectshape(1.0,false);
faces[1] = new Rectshape(1.0,false);
faces[2] = new Rectshape(1.0,false);
faces[3] = new Rectshape(1.0,false);
faces[4] = new Rectshape(1.0,false);
faces[5] = new Rectshape(1.0,false);


//// IRRELEVANT CODE ////
}

Rectshape::Rectshape(GLfloat size,bool hidden)
{
this->_topside = new Line(size,false);
this->_rightside = new Line(size,false);
this->_bottomside = new Line(size,false);
this->_leftside = new Line(size,false);

//// IRRELEVANT CODE ////

}

Line::Line(GLfloat size, bool hidden)
{
point1 = new Point(1.0,1.0,1.0,true);
point2 = new Point(1.0,1.0,1.0,true);


//// IRRELEVANT CODE ////
}

Point::Point(GLfloat x, GLfloat y, GLfloat z,bool hidden)
{
pos[0] = x;
pos[1] = y;
pos[2] = z;
oPos[0]=x;oPos[1]=y;oPos[2];
visible = !hidden;
}

With this approach I can easily create an entity and the constructors will do the rest since I have a function that automatically arranges points, rectangles, lines in a solid shape, no matter how far apart or how they're located in the space. I can access the solid class from Entity, has body is a solid object. But in order to access the rectangles that make up the solid, I need to do that thru the cube class directly! But I get uninitialized member variables and crashes when i call member functions that go beyond the solid class ( like rectangles).

For example, if I have the Entity class with body inside it, body is a pointer to a solid object (a cube in this case).
Though I can easily access the solid from a pointer to entity, I cannot seem to access the rectangles of which body is made of. I cannot seem to do this:

entityList[0]->body->faces[0]->function();

So how would I solve this problem?

PS: the "this" pointer is there because I copy pasted from another constructor in the same class.
The code is not complete because the issue doesn't seem to be in the source code itself, the problem is in the way I 'm trying to access the classes as It works just fine by defining every point, line, rectangle and then pass all those objects to the "default" constructor I made for the class. I just want to make this other way work because it is more convenient to me. The info I provided should be enough in order to address and solve the issue, hopefully.

Share this post


Link to post
Share on other sites
Advertisement
My first guess would be that you don't obey the Rule of Three in your classes. You're also abusing the living hell out of new, which is bad - you don't really need to "new" all those objects. Just make them value members instead of pointers, i.e. Rectangle instead of Rectangle* in your class definition. (Note that if you used value members, you wouldn't have to worry about the Rule of Three in this particular situation.)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!