开发者

Assign an object reference to a variable

I have a question about assign here, I want to know

  1. whether Assign makes a copy of the whole object and
  2. I would like to know whether FTEA.Objects[0] is also freed.

I want to make a copy of FTEA.Objects[0] and when I free ObjCur, I don't intend to free FTEA.Objects[0] - not sure the correct way of doing this, need your help, thanks :

function xxxxxxxxxxxxxxxxxxxxxxxx
var 
  curQuo, tempQuo:TXX_TEA;
begin
  curQuo :=TXX_TEA(FTEA.Objects[0]);
  if xxxxxxxxx then
  begin
    tempQuo := TXX_TEA.Create();
    tempQuo.Assign(curQuo);   // 开发者_Python百科Question 1: Assign copy the whole object value or not
    objCur.AddQuo(tempQuo)
  end
  else
    TXX_TEA(objCur.Quos[0]).Assign(curQuo);
  end;

  finally
    objCur.Free; // Question 2: FTEA.Objects[0] is freed or not
  end;
end


No, freeing one object that has been Assigned from another object does not free the source object.

How Assign will function depends entirely on the class. Each class has to implement Assign and then call inherited and assign its own custom properties from source to destination if the classes are compatible. I'd recommend looking at the source for these classes to check if they implement Assign.


Assign is a routine that must be implemented per class, so it does what it is programmed to do. It is supposed to try to copy the entire object that is passed as argument, as well as it can. But if it can't, it probably shouldn't copy anything.

Some classes may have implementations that do not conform to that unenforced "contract" and only copy the parts they need. In other words, what gets copied is completely up to the implementation.

Assign has no special meaning and is a normal function, not some kind of compiler magic. It does not do anything to the object passed as parameter, so freeing one object does not affect the other. The objects are totally independent.

Note that not all classes implement Assign. You may think you call the Assign for the class, but it may actually be the inherited Assign, which does not know anything about any new members in the derived class to be copied. The notion of Assign is nice, but unfortunately, it is often not implemented or not implemented properly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜