Jump to content
  • Advertisement
Sign in to follow this  
Overburn

Class interrelations problems

This topic is 3483 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

Ok, so I have 2 classes: CZRoot and CZMesh in CZRoot i have a Render() function that needs pointers to CZMesh objects to render them. in CZMesh i have a loadMesh() function that uses the CZRoot D3D device. I have a problem. how do I give each class what they need? i have thought of inheritance but it wouldn't really be a good design choice. can anyone help me on this? :D

Share this post


Link to post
Share on other sites
Advertisement
// mesh.hpp

// doesn't include root.hpp

// forward declaration
class CZRoot;

class CZMesh {
CZRoot * root;
};


You can optionally do the same for other classes.

Share this post


Link to post
Share on other sites
erm.. of course..
but i need to pass a pointer the other way around too, and it' kindof a chicken and egg problem.

CZRoot* root = new CZRoot();
CZMesh* mesh = new CZMesh(root);

, so i try this:
root->meshobject=mesh;

but unfortunately it gives a runtime error. anyone have any idea please? :)

Share this post


Link to post
Share on other sites
CZRoot* root = NULL;
CZMesh* mesh = new CZMesh(root);
root = new CZRoot(mesh);

Just make sure CZMesh does not try to use its CZRoot pointer in the constructor, as it'll still be NULL.

Share this post


Link to post
Share on other sites
Quote:
Original post by Mantear
CZRoot* root = NULL;
CZMesh* mesh = new CZMesh(root);
root = new CZRoot(mesh);

Just make sure CZMesh does not try to use its CZRoot pointer in the constructor, as it'll still be NULL.


It will still be null afterwards, too.

Refactor your design

Tight coupling like this is always a sign of wonky design. Try and find a way to design your code where these things do not need to know of each other. If such a link between two objects is necessity (ie, they have a relation with each other), then take a page out of the RDBMS book, and create a third class, which represents the relationship:

struct mesh_root_link
{
mesh_root_link(Mesh* mesh, Root* root)
: mesh(mesh), root(root)
{
}

// accessors and functions that require the knowledge of both of these things go here
private:
Mesh* mesh;
Root* root;
};



Share this post


Link to post
Share on other sites
thanx :) it kinda worked..
but i also have this little problem... as in i am doing something wrong here:
g_pd3dDevice->SetMaterial( &meshobject->g_pMeshMaterials );

it's a runtime one also. :(

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!