• Create Account

## How do I implement the singleton I describe without circular inclusion?

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

5 replies to this topic

### #1jdub  Members

Posted 24 September 2013 - 03:07 AM

I have a class called 'Window' which essentially contains everything need to run my program.  Inside window, I have 'Simulation', 'Renderer' and 'InputController' classes.  In some instances, these classes need access to a global pointer to a 'Window'.  For example:  I have a DrawDebug() function in 'Renderer' which needs to get information about the status of the Simulation so it can be drawn as text on screen.  I'm not sure exactly how to go about doing this and some help would be much appreciated.  Thanks

J.W.

### #2Álvaro  Members

Posted 24 September 2013 - 03:55 AM

POPULAR

First, let me say that your design is appalling. A window is a rectangular piece of screen, and it should know nothing about simulation, renderer or input controller. The fact that you sometimes need a pointer to a window means that everything inside window is effectively global, so you may as well make Simulation, Renderer and InputController global and stop pretending that you have some design.

Now, as to how to prevent circular inclusion, the header file for your Window class should probably include all the header files for the other classes, and the header files for the other classes should use a forward declaration, like this:
  class Window;
This will allow you to use pointers to Window without having to include the header file for Window.

### #3Muzzy A  Members

Posted 28 September 2013 - 06:47 PM

Why not just have this


// A function to get access to the singleton at anytime so there is no need for any parameters for the Window class
class Window
{
public:
static Window* getInstance()
{
static Window instance;
return &instance;

}
};

// You can also do it with dynamic memory instead of a static instance, which is preferred.


### #4yckx  Prime Members

Posted 04 October 2013 - 12:24 PM

Because few people would consider a window to be a good candidate for singleton usage.

### #5wintertime  Members

Posted 04 October 2013 - 12:38 PM

Yes, that looks so wrong. The window would be created at a totally random time or even never if noone asks for the pointer.

And later someone wants two windows...

### #6larspensjo  Members

Posted 08 October 2013 - 02:10 PM

Programming is much about keeping the number of dependencies as low as possible. With many dependencies, the source code is difficult to modify. When you find yourself in the situation that you have cyclic dependencies of header files, it is a sign that you may be doing something wrong. Of course, there are valid use cases, but I try to avoid them.

In worst case, using header guards, recursion can be avoided at the cost of strange compilation errors.

The use of forward declaration may solve this. It is actually a good general principle. Use it wherever possible, so as to avoid including header files.

Current project: Ephenation.
Sharing OpenGL experiences: http://ephenationopengl.blogspot.com/

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.