Eduardo Spaki
Eduardo Spaki

Reputation: 1188

Non-const function _fastcall TStrings::GetCount() called for const object

I have a old c++ code, wirited and compiled into c++ builder 5. But now, I need to update/migrate this code to c++ builder 2009. So, I have some problems:

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;
  }
}

At line "lngIndice = pList->Count;" I get this error: "E2522 Non-const function _fastcall TStrings::GetCount() called for const object".

So, how can I solve (work around) it?

Upvotes: 1

Views: 1056

Answers (1)

YePhIcK
YePhIcK

Reputation: 5856

Would help if you provided an exact definition of TStringList but I'll just assume it's a templatized array for the typename TString.

Work-around could be to cast away the const, as in:

lngIndice = (const_cast<TStringList*>(pList))->Count;

Of course it's just what it is - a work-around and you may want to look at providing a const-correct access function in TString itself instead

Upvotes: 3

Related Questions