Class interrelations problems

Started by
6 comments, last by Overburn 15 years, 4 months ago
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
Einstein once said that only the universe and human stupidity are infinite. He wasn't too sure about the universe though...
Advertisement
// mesh.hpp// doesn't include root.hpp// forward declarationclass CZRoot;class CZMesh {  CZRoot * root;};


You can optionally do the same for other classes.
thanx :) it worked
Einstein once said that only the universe and human stupidity are infinite. He wasn't too sure about the universe though...
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? :)
Einstein once said that only the universe and human stupidity are infinite. He wasn't too sure about the universe though...
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.
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 hereprivate:    Mesh* mesh;    Root* root;};



[ search: google ][ programming: msdn | boost | opengl ][ languages: nihongo ]
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. :(
Einstein once said that only the universe and human stupidity are infinite. He wasn't too sure about the universe though...
hmm, that's a good idea _goat. will try :) thanx
Einstein once said that only the universe and human stupidity are infinite. He wasn't too sure about the universe though...

This topic is closed to new replies.

Advertisement