Array of class intances
Regan Heath
regan at netmail.co.nz
Tue Apr 8 07:27:13 PDT 2008
Regan Heath wrote:
> YY wrote:
>> Suppose I have a class :
>>
>> class C {
>> int a;
>> int b;
>> this(int a1, int b1) {
>> a = a1;
>> b = b1;
>> }
>> }
>>
>> And I want to make an array of class C instances :
>>
>> C[] inst;
>> inst ~= new C(1,2);
>> inst ~= new C(3,4);
>>
>> What happened if I make a copy of inst using dup? Are the values or
>> the pointers are copied?
>>
>> C[] copyinst = inst.dup;
>>
>> When I tried to assert(inst.ptr == copyinst.ptr) it fails, which means
>> it copies the contents.
>
> This assert tells you that the data pointer of the arrays is not equal,
> which means each array has it's own copy of the class references.
>
> But, it doesn't mean the class instances themselves have been duplicated.
>
> This shouldn't fail:
>
> assert(inst[0] == copyinst[0]);
The above assert should of course read:
assert(inst[0] is copyinst[0]);
Regan
More information about the Digitalmars-d-learn
mailing list