class allocators should be more encapsulated

Thomas Kuehne thomas-dloop at kuehne.cn
Fri Dec 29 07:00:03 PST 2006


Luís Marques <luismarques at gmail.com> schrieb:
> Frits van Bommel wrote:
>> Yes they do take parameters, and the reason is indeed to customize how 
>> memory is allocated. But unless they throw an exception, they do have to 
>> actually _allocate_ some memory. If they don't throw, the return value 
>> must be a void* to a newly-allocated piece of memory.
>> So what I gather you're trying to do (potentially return a pointer to an 
>> already-existing object) isn't acceptable behavior for a custom allocator.
>
> You are right. If I return an existing object it will be initialized to 
> default values. I guess that means the solution to a singleton pattern 
> proposed by Burton Radons does not work 
> (http://www.digitalmars.com/pnews/read.php?server=news.digitalmars.com&group=D&artnum=14520)
>
> Still, it's a pity that "new ClassType()" cannot be used to 
> transparently return an existing object (conditionally or not).

Where is the problem?

#
# class Some{
#    int dummy;
# 
#    this(){
#       static Some existing;
#       if(existing is null){
#          existing = this;
#       }else{
#          this = existing;
#       }
#    }
# }
# 
# import std.stdio;
# 
# int main(){
#    Some a = new Some();
#    Some b = new Some();
#    a.dummy = 13;
#    writefln("b.dummy: %s", b.dummy);
# 
#    return 0;
# }
#

If you use this pattern alot, the GC will have to do some more cleaning.

Thomas




More information about the Digitalmars-d mailing list