Cross-referencing header files.

Hey all.

I’m trying to write my own (very simple) GUI framework. I’ve worked with flash alot, so i was
thinking of making it a flash-esque type of framework. I want to split the framework into
two base classes being UIComponent (Buttons, Sliders etc) and UIComponentContainer (Views, Panels etc).

The problem i’m having is that i would like to cross-reference these two classes. For example:

  
  
class UIComponent : public EventDispatcher  
{  
    public:  
        UIComponentContainer* m_Parent;  
};  
  
class UIComponentContainer : public UIComponent  
{  
    private:  
        std::list<UIComponent*> m_Children;  
};  
  

The reason for UIComponentContainer to extend UIComponent is that I want to keep most
of the functionallity in the component class and simply extend it so that it can also contain
other components.

I’ve been googling alot about this and have learned that you can forward declare classes,
so just before including a header file you declare the class inside the file to be included, like so

  
  
class UIComponentContainer;  
#include "UIComponentContainer.h"  
  

However I must be doing this wrong since it’s not helping at all, i still get the classic
cross-referencing error. How can I achieve this type of cross-referencing and is it a wise
thing to do at all or should I rethink the way I manage my components?

  • Edvin

Hi,

in UIComponent.h:

  
  
// forward declare of UIComponentContainer  
class UIComponentContainer;  
  
class UIComponent : public ... {};  
  

You may not use any methods of UIComponentContainer in the UIComponent-header.

in UIComponent.cpp

  
  
#include "UIComponentContainer.h"  
  

HTH,
Stephan

Thanks Stephan, that did the trick, the #include goes into the .cpp and not the .h, lesson learned.

  • Edvin

if you’d like to keep all your #includes inside your .h file, you can also just put the #include at the bottom of your .h… like so

UIComponent.h

  
  
// forward declare of UIComponentContainer  
class UIComponentContainer;  
  
class UIComponent : public ... {};  
  
#include "UIComponentContainer.h"