我有一个旧的 c++ 代码,编写并编译到 c++ builder 5 中。 但是现在,我需要将此代码更新/迁移到 c++ builder 2009。因此,我遇到了一些问题:
int __fastcall TAllConversor::ListToStr(
const TStringList* pList,
AnsiString& strValue,
const long lngLimiteInferior,
const long lngLimiteSuperior) const
{
long lngIndice;
AnsiString strAux;
try
{
if (lngLimiteSuperior == 0)
lngIndice = pList->Count;
else
lngIndice = lngLimiteSuperior + lngLimiteInferior;
for (int i = lngLimiteInferior; i < lngIndice; i++)
{
strAux += pList->Strings[i] + ";";
}
strValue = strAux;
return 1;
}
catch(...)
{
return 0;
}
}
在行“lngIndice = pList->Count;”我收到此错误:“E2522 非 const 函数 _fastcall TStrings::GetCount() 调用了 const 对象”。
那么,我该如何解决(解决)它呢?
最佳答案
如果您提供 TStringList 的确切定义会有所帮助,但我将假设它是类型名称 TString 的模板化数组。
变通方法可能是放弃常量,如:
lngIndice = (const_cast<TStringList*>(pList))->Count;
当然,它就是它的本来面目 - 一种变通方法,您可能希望改为在 TString 本身中提供一个 const-correct 访问函数
关于c++ - 非 const 函数 _fastcall TStrings::GetCount() 为 const 对象调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11453330/