Class interrelations problems
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
// mesh.hpp// doesn't include root.hpp// forward declarationclass CZRoot;class CZMesh { CZRoot * root;};
You can optionally do the same for other classes.
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? :)
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? :)
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 MantearCZRoot* 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;};
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement