Sign in to follow this  

Class interrelations problems

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

This topic is 3294 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this