Re: [Dev-C++] Design decision in C++
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Per W. <pw...@ia...> - 2004-03-10 06:09:33
|
The only real advantage of a static object is that you don't allocate it which might be an advantage in a small embedded environment - you might write the application without support for dynamic memory. If the class on the other hand has only static members, you don't have to send the "this" pointer to the member functions. A very minor improvement that is mostly eaten up for a number of compilers by loss of a couple of compiler optimizations specially added for C++ object accesses. If the class is using static methods it has to use static member variables too, which is a bit bothersome, and you don't get to use a constructor. Some notable advantages of Singleton objects are: 1) Constructor is run after main, so it might be easier to debug. 2) Programmer control when constructor is run, which is advantageous if multiple classes relates to each other, and you want to control order of construction. 3) If you later realizes that you need more than one object of a class, it tends to be easier to modify the program if the class was originally using the Singleton pattern. A solution with static methods requires heavy rewrites. /Per W On Tue, 9 Mar 2004, Doug MacDonald wrote: > This is thread is especially interesting to me, as I'm working on a > project that uses Singleton for a number of its classes, and I'm not > sure it is warranted. No one has addressed Michael's original question: > what are the tradeoffs between using Singleton and just making the class > (or all of its members) static? I'd like to know about others' > experiences with Singleton, in case it's advisable to move from > Singleton-based classes to static classes in my project. > > Doug > > > > |