Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Code cleanup branch
08-06-2006, 02:43 PM,
#24
 
joevenzon Wrote:Leave them as globals. :-)

Quote:The thing I like about singletons is they are created on first use

... that sort of "feature" seems pretty annoying if you're trying to figure out when your data is actually being created, since it will just be buried within whatever function happens to call a GetInstance() type function first.

So, the things I don't like about singletons:

Quote:1) extra typing

Singleton's create minimal extra typing. And with the benefits singletons give you, I think the extra typing is worth it.

Quote:2) the data is created whenever the singleton is first accessed (which is not easy to find out)

It is easy to find out, and that data is created after main(). Unlike globals, you can know and control exactly when the data is created. Also, well designed global/singleton data should not be overly depenendent on other globals/singletons. A good singleton is designed so that it does not matter when it is first created, although this is not always possible.

Another advantage of singletons is eaiser error handling. Using globals, you cannot use exceptions to signal error conditions during construction and the only way to accomplish this sort of thing would be to have a two step construction process which is unnecessary. With a singleton, because it gets created in the main() function, you can catch any exceptions that may be thrown during construction. This is not possible with globals.

Quote:3) they use static data

I see no problems with this.

One other advantage singletons provide is ensuring that a global data member is only created once and only once.

These three questions and answers from the c++ faq, discuss this issue and potential problems with several possible solutions.
http://www.parashift.com/c++-faq-lite/ct...#faq-10.12
http://www.parashift.com/c++-faq-lite/ct...#faq-10.13
http://www.parashift.com/c++-faq-lite/ct...#faq-10.14

A good post and thread on the subject:
http://www.codeguru.com/forum/showpost.p...stcount=11
Reply


Messages In This Thread
Code cleanup branch - by thelusiv - 08-01-2006, 01:46 AM
[No subject] - by FFuser - 08-01-2006, 05:34 AM
[No subject] - by joevenzon_phpbb2_import3 - 08-01-2006, 08:28 PM
[No subject] - by thelusiv - 08-01-2006, 08:39 PM
[No subject] - by alex25 - 08-01-2006, 11:38 PM
[No subject] - by thelusiv - 08-03-2006, 12:31 AM
[No subject] - by joevenzon_phpbb2_import3 - 08-03-2006, 10:28 PM
[No subject] - by thelusiv - 08-03-2006, 10:33 PM
[No subject] - by thelusiv - 08-03-2006, 10:33 PM
[No subject] - by matthew_i - 08-04-2006, 01:20 AM
[No subject] - by matthew_i - 08-04-2006, 01:20 AM
[No subject] - by thelusiv - 08-04-2006, 01:38 AM
[No subject] - by thelusiv - 08-04-2006, 01:38 AM
[No subject] - by matthew_i - 08-04-2006, 01:41 AM
[No subject] - by matthew_i - 08-04-2006, 01:41 AM
[No subject] - by thelusiv - 08-04-2006, 01:53 AM
[No subject] - by thelusiv - 08-04-2006, 01:53 AM
[No subject] - by matthew_i - 08-04-2006, 01:58 AM
[No subject] - by matthew_i - 08-04-2006, 01:58 AM
[No subject] - by thelusiv - 08-04-2006, 09:08 AM
[No subject] - by thelusiv - 08-04-2006, 09:08 AM
[No subject] - by joevenzon_phpbb2_import3 - 08-04-2006, 08:38 PM
[No subject] - by joevenzon_phpbb2_import3 - 08-04-2006, 08:38 PM
[No subject] - by matthew_i - 08-06-2006, 02:43 PM
[No subject] - by matthew_i - 08-06-2006, 02:43 PM
[No subject] - by joevenzon_phpbb2_import3 - 08-06-2006, 07:45 PM
[No subject] - by joevenzon_phpbb2_import3 - 08-06-2006, 07:45 PM
[No subject] - by matthew_i - 08-06-2006, 11:25 PM
[No subject] - by matthew_i - 08-06-2006, 11:25 PM
[No subject] - by joevenzon_phpbb2_import3 - 08-07-2006, 09:26 AM
[No subject] - by joevenzon_phpbb2_import3 - 08-07-2006, 09:26 AM
[No subject] - by thelusiv - 08-07-2006, 04:43 PM
[No subject] - by thelusiv - 08-07-2006, 04:43 PM
[No subject] - by clytle374 - 08-07-2006, 04:51 PM
[No subject] - by clytle374 - 08-07-2006, 04:51 PM
[No subject] - by thelusiv - 08-07-2006, 06:25 PM
[No subject] - by thelusiv - 08-07-2006, 06:25 PM
[No subject] - by clytle374 - 08-08-2006, 12:47 AM
[No subject] - by clytle374 - 08-08-2006, 12:47 AM
[No subject] - by thelusiv - 08-08-2006, 09:05 AM
[No subject] - by thelusiv - 08-08-2006, 09:05 AM
[No subject] - by clytle374 - 08-08-2006, 07:19 PM
[No subject] - by clytle374 - 08-08-2006, 07:19 PM

Forum Jump:


Users browsing this thread: 7 Guest(s)