Skip to main content
gamedev.net
🔒 Locked

Class interrelations problems

Started by Overburn Dec 4, 2008 at 12:28 PM 6 replies 850+ views
Original Post
Overburn
Overburn
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...
Antheus
Antheus
// mesh.hpp// doesn't include root.hpp// forward declarationclass CZRoot;class CZMesh {  CZRoot * root;};


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



Overburn
Overburn
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...
Overburn
Overburn
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...

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.