为什么 Delphi 中的类不能使用运算符重载?
我一直想知道这个问题,但为什么 Delphi 中的类不能使用运算符重载?
我记得在逃亡时读过一次答案,它说它会与某些东西发生冲突,但我记不太清了。据我所知,只有隐式运算符可能会导致一些问题,因为类存储在堆上,并且分配实际上是堆地址的副本(基本上是复制指针)。
I've always wondered about this for a bit, but why is operator overloading not available for classes in Delphi?
I remember reading an answer once while on the run, and it said it would come in conflict with something, but I can't remember much. As far as I can tell, only the implicit operator may cause a bit of problems since classes are stored on the heap and assigning is actually a copy of the heap address (basically copying pointers).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
关闭。这是因为对象是引用类型并且内存是手动管理的。因此,如果您说 myResult := myObject1 + myObject2 + myObject3;,则必须在其中的某个位置创建一个中间对象,并且没有代码来释放它,因此会出现内存泄漏。
Close. It's because objects are reference types and the memory is managed manually. So if you said
myResult := myObject1 + myObject2 + myObject3;
, you'd have to create an intermediate object somewhere in there, and there's no code to free it, so you get memory leaks.您可以对类进行运算符重载,但仅限于类使用 ARC 的 NextGen 编译器。
请参阅:http://blog.marcocantu.com/blog/class_operators_delphi.html
在 XE5 中引入,请参阅:Delphi 语言功能列表以及引入/弃用它们的版本
You can have operator overloading for classes, but only for the NextGen compiler where classes use ARC.
See: http://blog.marcocantu.com/blog/class_operators_delphi.html
This was introduced in XE5, see: List of Delphi language features and version in which they were introduced/deprecated
梅森惠勒说这是不可能的,因为中间对象管理。
但根据 Embarcadero 文档,Delphi 2009 中可以使用类运算符。
RAD Studio 2009 - 运算符重载
自 Delphi 7 以来的新 Delphi 语言功能
Mason Wheeler says that it's impossible because intermediate object management.
But according to Embarcadero documentation, class operators where possible with Delphi 2009.
RAD Studio 2009 - Operator Overloading
New Delphi language features since Delphi 7