dewdman42@gmail .com wrote:[color=blue]
> I have a question for you C++ gurus. Let's say I have a class
> singleton class such as:
>
> class singleton
> {
> .
> .
> public:
> singleton* getInstance();
> private:
> static singleton* __instance;
> .
> .
> }
>
> singeton.cpp:
>
> singleton* singleton::__in stance;
>
> singleton* getInstance()
> {
> if (__instance==nu ll) {
> __instance = new singleton;
> }
>
> return __instance;
> }
>[/color]
recommendation:
make instance a static variable inside getInstance(). That will provide
a cleaner way of ensuring initialization.
Also, and this is important, your entire example is undefined behavior.
The standard reserves all identifiers containing a double-underscore
regardless of scope to the implementation (see 17.4.3.1.2).
So try:
class singleton
{
public:
// all other member functions redacted for clarity
static singleton* getInstance();
};
singleton* singleton::getI nstance()
{
// of course, you'll need appropriate threading safeguards here
static singleton* instance = 0;
if (!instance)
instance = new singleton;
return instance;
}
In addition, Alexandrescu's "Modern C++ Design" will tell you more than
you ever wanted to know about Singleton implementation.
red floyd wrote:[color=blue]
> recommendation:
>
> make instance a static variable inside getInstance(). That will provide
> a cleaner way of ensuring initialization.
>
> Also, and this is important, your entire example is undefined behavior.
> The standard reserves all identifiers containing a double-underscore
> regardless of scope to the implementation (see 17.4.3.1.2).
>
> So try:
>
> class singleton
> {
> public:
> // all other member functions redacted for clarity
> static singleton* getInstance();
> };
>
> singleton* singleton::getI nstance()
> {
> // of course, you'll need appropriate threading safeguards here
> static singleton* instance = 0;
> if (!instance)
> instance = new singleton;
> return instance;
> }
>
>
> In addition, Alexandrescu's "Modern C++ Design" will tell you more than
> you ever wanted to know about Singleton implementation.[/color]
Hi
I have one question regarding the singleton implementation: is it
preferable to do like the above implementation or something like:
ferdinand.stefa nus@gmail.com wrote:[color=blue]
>
> I have one question regarding the singleton implementation: is it
> preferable to do like the above implementation or something like:
>
> singleton& singleton::getI nstance()
> {
> static singleton instance;
> return instance;
> }
>[/color]
Given the following disclaimer: I'm just an intermediate hack... I'm not
a guru like Victor, Pete, P.J., or some of the other regulars here; my
recommmendation is:
I'd return the reference. You avoid some issues with initializing
"instance", you don't have to worry about deleteing the pointer, and you
don't have to worry about new throwing std::bad_alloc.
Again, I highly recommend the singleton chapter of MCPPD, even though it
is advanced reading.
ferdinand.stefa nus@gmail.com wrote:[color=blue]
> red floyd wrote:[color=green]
>> recommendation:
>>
>> make instance a static variable inside getInstance(). That will provide
>> a cleaner way of ensuring initialization.
>>
>> Also, and this is important, your entire example is undefined behavior.
>> The standard reserves all identifiers containing a double-underscore
>> regardless of scope to the implementation (see 17.4.3.1.2).
>>
>> So try:
>>
>> class singleton
>> {
>> public:
>> // all other member functions redacted for clarity
>> static singleton* getInstance();
>> };
>>
>> singleton* singleton::getI nstance()
>> {
>> // of course, you'll need appropriate threading safeguards here
>> static singleton* instance = 0;
>> if (!instance)
>> instance = new singleton;
>> return instance;
>> }
>>
>>
>> In addition, Alexandrescu's "Modern C++ Design" will tell you more than
>> you ever wanted to know about Singleton implementation.[/color]
>
> Hi
>
> I have one question regarding the singleton implementation: is it
> preferable to do like the above implementation or something like:
>
> singleton& singleton::getI nstance()
> {
> static singleton instance;
> return instance;
> }
>
> Thanks!
>[/color]
Return the reference (last proposal).
The "pointer version" would make sense if it is a lot of effort/time
needed to construct the instance and the instance isn't needed in every
program run. But even the return a reference:
singleton& singleton::getI nstance()
{
// of course, you'll need appropriate threading safeguards here
static singleton* instance = 0;
if (!instance)
instance = new singleton;
return *instance;
}
--
Who is General Failure and why is he reading my hard disk?
On Tue, 24 Jan 2006 09:50:43 +0100, Gabriel wrote:
[color=blue]
> ferdinand.stefa nus@gmail.com wrote:[color=green]
>>
>>[/color]
> Return the reference (last proposal).
>
> The "pointer version" would make sense if it is a lot of effort/time
> needed to construct the instance and the instance isn't needed in every
> program run. But even the return a reference:
> singleton& singleton::getI nstance()
> {
> // of course, you'll need appropriate threading safeguards here
> static singleton* instance = 0;
> if (!instance)
> instance = new singleton;
> return *instance;
> }[/color]
But this version has the same behavior (object only constructed when
getInstance first called), and the object is automatically destroyed as
well.
Is there any reason to prefer new over a static variable (besides the fact
that the "new"d version will outlive anyone potentially using it; that is,
there are no program termination issues)?
Jay Nabonne wrote:[color=blue]
> On Tue, 24 Jan 2006 09:50:43 +0100, Gabriel wrote:
>[color=green]
>> ferdinand.stefa nus@gmail.com wrote:[color=darkred]
>>>[/color]
>> Return the reference (last proposal).
>>
>> The "pointer version" would make sense if it is a lot of effort/time
>> needed to construct the instance and the instance isn't needed in every
>> program run. But even the return a reference:
>> singleton& singleton::getI nstance()
>> {
>> // of course, you'll need appropriate threading safeguards here
>> static singleton* instance = 0;
>> if (!instance)
>> instance = new singleton;
>> return *instance;
>> }[/color]
>
> But this version has the same behavior (object only constructed when
> getInstance first called), and the object is automatically destroyed as
> well.
>
> singleton& singleton::getI nstance()
> {
> static singleton instance;
> return instance;
> }
>
> Is there any reason to prefer new over a static variable (besides the fact
> that the "new"d version will outlive anyone potentially using it; that is,
> there are no program termination issues)?
>
> - Jay
>[/color]
Oh yes, you're right. I didn't take enough time to think the problem
through right. So I forgot about destruction and mixed the exact time of
creation up.
--
Who is General Failure and why is he reading my hard disk?
Comment