У меня есть старый код c++, который является методом const:
Id LegacyCode::GetIdByName(const char* sName,
const char* pName) const
{
long lID;
char szProcessName[MAXPATH];
for (int iP = 0;
iP < iNum;
iP++) {
if (strnicmp(sName, "Test.exe", MAX_LENGTH) == 0)
{
if (strnicmp(szProcessName, pName, MAX_LENGTH) == 0)
{
// If the proces name was found, return its ID.
return (lID);
}
}
else if (strnicmp(sName, "Test2.exe", MAX_LENGTH) == 0)
{
if (strnicmp(szProcessName, pName, MAX_LENGTH) == 0)
{
// If the proces name was found, return its ID.
return (lID);
}
}
else
{
if (strnicmp(sName, szProcessName, MAX_LENGTH) == 0)
return (lID);
}
}
return (0);
}
и мне нравится заменить его более коротким кодом, подобным этому, используя вспомогательную функцию
Id LegacyCode::GetIdByName(const char* sName,
const char* pName) const
{
long lID;
char szProcessName[MAXPATH];
for (int iP = 0;
iP < iNum;
iP++) {
if (isProcessName(sName,pName,szProcessName))
{
return (lID);
}
else
{
if (strnicmp(sName, szProcessName, MAX_LENGTH) == 0)
return (lID);
}
}
return (0);
}
bool LegacyCode::isProcessName(char* _sName,char* _pName,char* _szProcessName)
{
if (strnicmp(_sName, "Test.exe", MAX_LENGTH) == 0)
{
if (strnicmp(_szProcessName, pName, MAX_LENGTH) == 0)
{
// If the proces name was found, return its ID.
return (lID);
}
}
else if (strnicmp(sName, "Test2.exe", MAX_LENGTH) == 0)
{
if (strnicmp(_szProcessName, pName, MAX_LENGTH) == 0)
{
// If the proces name was found, return its ID.
return (lID);
}
}
return false;
}
но, к сожалению, я получаю ошибку const:
error C2662: 'LegacyCode::isProcessName' : cannot convert 'this' pointer from 'const LegacyCode' to 'LegacyCode &'
есть ли способ преодолеть это, не добавляя mutable к функциональным мембранам?
Сделать LegacyCode::isProcessName
const:
bool LegacyCode::isProcessName(char* _sName,char* _pName,char* _szProcessName) const;