...has exited with code 1 (0x1).

Started by
11 comments, last by nmi 18 years, 10 months ago
Quote:Original post by Mizipzor
It works now but is that the best way to solve it?


First of all, using cout to display debugging messages is bad, since cout buffers output. So either flush cout after the message, or use cerr (or clog) for debugging.

The next thing are global objects. Since the objects are available from everywhere, it is easy to create a cyclic dependency between those objects by accident. One thing to solve this is to have an init() function, that must be called before the object can be used. The constructor only initialises the internal state of the object, i.e. variables that depend in no way on other objects. So the initialisation is a two step approach, first create the object via its ctor, second initialise it via init().
(Singletons hide cyclic dependency between objects.)

Another way to manage those global objects is to put them into one single class, lets call it 'Globals'. The order of initialisation of objects in this class is determined by the standard, so you don't have to use singletons. Then there is only one instance of Globals, so you can use the objects in this class without a helper function like getInstance(). (BTW its always good to keep all global objects at one place.)

Its also possible to have no global objects at all (and no singletons). I think this will also lead to a better design. So if you can do it this way, go for it. Object dependency is like a tree. There is not a case such as the game needs the console to write messages, but the console needs the game to actually show them. You create your main class (you called it Game) either via new, or as a local object in main(). The ctor of that class then creates the other objects and so on.
Advertisement
Ok... ill try to move all objects in a GlobalObjects class, then get rid of all the singleton stuff and make the Global class have functions to retrieve the instance of each object.

Thanks for all your help. :)

[edit] You mean like this? Quite ugly code though... I must say :P

#ifndef _GLOBAL_OBJECTS#define _GLOBAL_OBJECTS#include "AsciiMap.h"#include "Console.h"#include "FontSystem.h"#include "SdlHandle.h"class GlobalObjects {private:	// all the objects needed for the game	AsciiMap	AsciiMap;	Console		Console;	FontSystem	FoFontSystemnt;	SdlHandle	SdlHandle;public:	// get the instance of every class	AsciiMap*	GetAsciiMapInstance	(&AsciiMap	);	Console*	GetConsoleInstance	(&Console	);	FontSystem*	GetFontSystemInstance	(&FontSystem    );	SdlHandle*	GetSdlHandleInstance	(&SdlHandle	);};#endif
Yes, something like that. But you may want to return references instead of pointers. (Or you can make those members public.) The functions for getting the global objects should be inline functions, so the compiler can optimise this, as if there wasn't any function call.

Another thing you need to do is how to obtain the instance of GlobalObjects. Either create an (inline) function, that returns a reference to the global object like this:
inline GlobalObjects& getGlobals() {  extern GlobalObjects globals;  // define it in some .cc file  return globals;}


Or you can just assume that it has a fixed name, and access it directly by this name.

This topic is closed to new replies.

Advertisement