January 24, 2013 Re: Singleton Pattern with struct | ||||
---|---|---|---|---|
| ||||
Posted in reply to Maxim Fomin | On Thursday, 24 January 2013 at 16:49:53 UTC, Maxim Fomin wrote:
> On Thursday, 24 January 2013 at 16:17:34 UTC, Era Scarecrow wrote:
>> Hmmm. You could separate the data and remove the pointer... then use alias this.
>>
>> [code]
>> struct Singleton {
>> static SingletonData single;
>> alias single this;
>> @disable this(this);
>> //disable copy/assignment? Not that it would matter...
>> //copying nothing does nothing, as there's nothing here.
>>
>> private static struct SingletonData {
>> //methods and data here.
>> }
>> [/code]
>
> I agree, but disabling ctors makes creating module-level object tricky. Static struct initialization might help.
I'll have to agree. But in that case don't disable the default ctor (not that the Singleton has any contents anyways); Actually opAssign/postblit only needs to be disabled inside SingletonData. I'd think that would work.
[code]
struct Singleton {
static SingletonData single;
alias single this;
private static struct SingletonData {
@disable void opAssign();
//methods and data here.
}
}
[/code]
|
Copyright © 1999-2021 by the D Language Foundation