c++ - 非 const 函数 _fastcall TStrings::GetCount() 为 const 对象调用

标签 c++ constants c++builder

我有一个旧的 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/

相关文章:

c++ - 是否存在加载多个 mscvrt**.dll 版本的问题?

c++ - 宏 INT_MAX 在哪里定义的?

将闪存中的 const 结构复制到 RAM 中的 "normal"结构

mysql - 使用 TSQLConnection 和 SSL 连接到 Mysql

c++ - 我需要转换无符号字符吗?

c++ - GDI 屏幕截图,结果在不同的计算机上有所不同

c++ - 更改 volatile const 的值 - G++ 与 Visual Studio 2019

C - 保护持久设置

delphi - TIdTCPServer 如何在 60Hz 计时器中向所有客户端多播?

C++ Builder - Lib 和 Res 的区别