Re: Single Object Instance Techniques
Dnia Sat, 17 Mar 2007 02:48:54 +0000, JohnQ napisa?(a):
Why would anyone write:
class SomeThing
{
private:
SomeThing();
static SomeThing* pInstance_;
public:
static SomeThing* getInstance()
{
if (!pInstance_)
{
pInstance_ = new SomeThing();
}
return pInstance_;
}
};
SomeThing* SomeThing::pInstance_ = 0;
Maybe because he's a stuntman ;)
Think about where is a delete operator called for the
object pointed by pInstance_ ? When is a destructor called?
Answer is: never :P and it's not good :/
The other flaw is returning a pointer to internal data,
because it gives an opportunity for someone to call 'delete'
on this pointer and deallocate that internal, private object.
There is also nothing to stop you from copy-construct the
additional objects of that class, because copy constructor
wasn't declared private.
Instead, I advice you the Meyers's approach:
class SomeThing
{
public:
// Returning by reference ensures that calling delete
// on the returned object will cause a compiler error.
static SomeThing& getInstance()
{
// The following object will be allocated in a static
// memory, but initialized only once, upon first call
// of this function, with the use of private constructor.
// Further calls will only return that single instance.
// Object will be properly destructed [using destructor]
// when the program ends.
static SomeThing instance;
return instance;
}
// ...other public interface...
private:
// Declaring the following as private prevents illegal
// creations of more instances.
SomeThing();
SomeThing(const SomeThing&);
SomeThing& operator=(const SomeThing&);
};
Example of use:
SomeThing obj = SomeThing::getInstance();
obj.someMethod();
delete obj; //error: not a pointer :)
SomeThing::getInstance().someMethod();
class SomeThing // class unencumbered by single-instancing concerns :)
If you don't want only one single instance, but you only want to
have global access to that object and prevent static initialization
order fiasco, you don't need that pattern.
SomeThing* GetSomeThing() // single-instancing accessor function
Accessor? But it's not a member of any class.
I would rather call it factory/creation function ;)
{
static SomeThing* thing = new SomeThing();
return thing;
}
Yes, that function returns always the same one instance of
SomeThing, but doesn't prevent from creating more instances
in the other place in a program.
Single-instance techniques help with the initialization
order problem
But not ONLY with that ;)
and the second form works great for that.
It's not. Still someone may try to create some global
instances depending on other global objects in a program
[and their initialization before theirs].
I don't think single instance control belongs in a
domain class.
And what do you think about a cloning/copying domain? :)
'SingleInstancer' should be a template class.
Or an interface. But it's not that simple.
--
SasQ