德尔福中C++“const”返回类型相当于什么

What's the equivalent of C++ `const` return types in Delphi

本文关键字:返回类型 相当于 什么 const C++ 德尔福      更新时间:2023-10-16

我有以下C++代码:

标题:(在类内)

virtual const bigint &getPopulation() ;

实现:

static bigint negone = -1 ;
const bigint &hlifealgo::getPopulation() {
   // note:  if called during gc, then we cannot call calcPopulation
   // since that will mess up the gc.
   if (!popValid) {
      if (inGC) {
        needPop = 1 ;
        return negone ;
      } else {
        calcPopulation(root) ;
        popValid = 1 ;
        needPop = 0 ;
      }
   }
   return population ;
}

我把它移植到德尔福,它工作得很好。我仍然对常量返回类型感到有些困惑。

我可以忽略翻译中的const,还是这里有什么需要注意的?

德尔福有这个概念的类似物吗?

在德尔福没有类似的内容。您在这里拥有的是const参考。在德尔福中,没有区分可变引用和常量引用的机制。所有引用都可用于改变对象。因此,这不是与const返回类型相关的特别问题,而是Delphi不支持常量引用。

在将代码从C++移植到 Delphi 时,您别无选择,只能忽略const引用。您无法区分德尔福中不同类型的引用,只有一个。