为变量分配对象引用

时间:2021-07-06 23:34:23

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

我有一个关于分配的问题,我想知道

  1. whether Assign makes a copy of the whole object and
  2. Assign是否复制整个对象和

  3. I would like to know whether FTEA.Objects[0] is also freed.
  4. 我想知道FTEA.Objects [0]是否也被释放。

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 :

我想制作FTEA.Objects [0]的副本,当我释放ObjCur时,我不打算释放FTEA.Objects [0] - 不确定这样做的正确方法,需要你的帮助,谢谢:

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);   // 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

2 个解决方案

#1


3  

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.

Assign是一个必须按类实现的例程,因此它按照程序编程执行。它应该尝试复制作为参数传递的整个对象,以及它可以。但如果它不能,它可能不应该复制任何东西。

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.

请注意,并非所有类都实现Assign。您可能认为您为类调用了Assign,但它实际上可能是继承的Assign,它不知道要复制的派生类中的任何新成员。 Assign的概念很不错,但不幸的是,它通常没有实现或没有正确实现。

#2


5  

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,然后调用inherited并在类兼容时从源到目标分配自己的自定义属性。我建议查看这些类的源代码,以检查它们是否实现了Assign。

#1


3  

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.

Assign是一个必须按类实现的例程,因此它按照程序编程执行。它应该尝试复制作为参数传递的整个对象,以及它可以。但如果它不能,它可能不应该复制任何东西。

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.

请注意,并非所有类都实现Assign。您可能认为您为类调用了Assign,但它实际上可能是继承的Assign,它不知道要复制的派生类中的任何新成员。 Assign的概念很不错,但不幸的是,它通常没有实现或没有正确实现。

#2


5  

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,然后调用inherited并在类兼容时从源到目标分配自己的自定义属性。我建议查看这些类的源代码,以检查它们是否实现了Assign。