This topic is now archived and is closed to further replies.


Circular references between objects

Recommended Posts

Hi! I''m currently in the process of writing a game which is quite advanced on the gameplay side. I have been working on the GUI part of it right now (implementing different type of controls) for the different screens. I have a class that I called Gui that represents a container of controls. The abstract control class is called VisualComponent. A VisualComponent must have a pointer on the Gui which contains it. This is necessary, and it''s my problem.. : ) I need that because when the user clicks on a component the component must be able to requestFocus() to the gui. The hasFocus() delegates also to the Gui to know if the component has the focus or not (identity test). So in Gui.h I have included "VisualComponent.h" and in VisualComponent.h I have included "Gui.h".. but the compiler does not like that. I do not understand how I could do that without this circular reference. Most GUI framework use that (I worked mostly in Smalltalk and Java/Swing). Any help will be greatly appreciated. The .h files go like this: ////////////////////////////////////// // Gui.h: interface for the Gui class. // ////////////////////////////////////// #if !defined(AFX_GUI_H__26233576_B1EC_4C9A_8443_27C3A579EF1B__INCLUDED_) #define AFX_GUI_H__26233576_B1EC_4C9A_8443_27C3A579EF1B__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 #include #include "VisualComponent.h" #include "MouseCursor.h" #include "Label.h" #include "TextField.h" #include "Button.h" class Gui{ public: Gui(); virtual ~Gui(); ... void requestFocus(VisualComponent* component); void hasFocus(VisualComponent* component); ... private: typedef std::vector Components; Components components; VisualComponent* componentWithFocus; ... }; #endif // !defined(AFX_GUI_H__26233576_B1EC_4C9A_8443_27C3A579EF1B__INCLUDED_) ////////////////////////////////////////////////////////////// // VisualComponent.h: interface for the VisualComponent class. // ////////////////////////////////////////////////////////////// #if !defined(AFX_VISUALCOMPONENT_H__EE8F0CCC_ABCB_4C27_9F9B_9EC0F92845A6__INCLUDED_) #define AFX_VISUALCOMPONENT_H__EE8F0CCC_ABCB_4C27_9F9B_9EC0F92845A6__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 class VisualComponent{ public: VisualComponent(); virtual ~VisualComponent(); ... void requestFocus(); void hasFocus(); ... private: Gui* parent; ... }; #endif // !defined(AFX_VISUALCOMPONENT_H__EE8F0CCC_ABCB_4C27_9F9B_9EC0F92845A6__INCLUDED_) Thanks in advance, Patrick Lafleur System Engineer Creation Objet Inc.

Share this post

Link to post
Share on other sites