我有以下 C++ 代码:
标题:(在类内)
virtual const bigint &getPopulation() ;
Run Code Online (Sandbox Code Playgroud)
执行:
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 ;
}
Run Code Online (Sandbox Code Playgroud)
我把它移植到 Delphi,它工作得很好。我仍然对 const 返回类型感到有些困惑。
我可以忽略const翻译吗,或者这里有什么需要注意的吗?
Delphi 中是否有类似的概念?
在 Delphi 中没有类似的东西。你这里有什么是const参考。在 Delphi 中没有区分可变引用和常量引用的机制。所有的引用都可以用来改变一个对象。所以这不是一个特别与const返回类型相关的问题,更多的是 Delphi 不支持常量引用。
const将代码从 C++ 移植到 Delphi 时,您别无选择,只能忽略引用。您无法区分 Delphi 中不同类型的引用,只有一种。