quote:Original post by JohnBSmall I''d be interested to hear what makes using singleton classes better than using namespaces.
By that I assume you mean a singleton as opposed to a global object or group of variables encapsulatied in a namespace. If not, explain a little further.
--
There are several reasons. The three that stand out immediately to me are:
Dynamic Construction and Destruction : You many times won''t need the object/data for the entire duration of the program so it''s good to be able to construct and destruct the object manually, especially if the object takes up a lot of space in memory.
Order of Construction : Let''s say you have global data in two separate files, but the construction of one requires data directly or indirectly from the other group. For instance, what if you had a global window object and a global renderer object in different files. Let''s say the renderer needs to be associated with a window. It''s hard to be certain which object is being created first, but in order for your program to work properly, it is essential that the window is created first. With 2 singletons you can construct and destruct them in the order you want in whatever functions you want.
Varying Constructors/Arguments : You may want your object to be initialized with different values depending on user input, etc. If it was a fixed global object then you''d be limited to a single construction with fixed parameters, which you often times don''t want.