Jump to content
  • Advertisement


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


Circular references between objects

This topic is 6357 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Forward declarations.

// file1.h

class foo;

class bar {
foo* f;

// file2.h

class bar;

class foo {
bar* b;

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!