为什么在Delphi中类不能使用运算符重载? [英] Why isn't operator overloading available for classes in Delphi?

查看:240
本文介绍了为什么在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).

推荐答案

关闭。这是因为对象是引用类型,并且内存是手动管理的。所以如果你说 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.

这篇关于为什么在Delphi中类不能使用运算符重载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆