Original Post
Hi all - at first sorry for my poor english... Lets assume that we have two basic scene graph classes CNode and CScene. Due to design issues they must know following: Is there any clear way to implement that situation?
class CScene
{
protected
// CScene must know all of its children:
CList<CNode*> _Children;
public
// It alsmo must be able to add new children:
void AddNode(CNode* Node)
{
Node->SetScene(this);
_Children.Add(Node);
}
}
class CNode
{
protected
// CNode must know its parent scene:
CScene* _Scene;
public
// PROBLEM
// This method must be public, because it is called in
// CScene::AddNode()
// But end user SHOULD NOT HAVE ACCES TO IT, because it is
// only usable in CScene::AddNode(), where _Children.Add()
// is also called (without this it has only 'half' of functionality)
void SetScene(CScene* Scene)
{
_Scene = Scene;
}
// So, SOLUTION 1:
// Leave this method public and make it fully functional:
// PROBLEM:
// Of course, infinite recursion
void SetScene(CScene* Scene)
{
_Scene = Scene;
Scene->AddNode(this);
}
// So, SOLUTION 2:
// Make this method fully functional and add additional bool parameter,
// inside CScene::AddNode() set it to false, outside to true (default)
// PROBLEM:
// End user have an additional, confusing parameter
void SetScene(CScene* Scene, bool Param = true)
{
_Scene = Scene;
if (Param)
{
Scene->AddNode(this);
}
}
}