use*_*898 0 c++ const function
我有遗留的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);
}
Run Code Online (Sandbox Code Playgroud)
我喜欢用更好的代码替换它,使用辅助函数
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;
}
Run Code Online (Sandbox Code Playgroud)
但不幸的是我得到const错误:
error C2662: 'LegacyCode::isProcessName' : cannot convert 'this' pointer from 'const LegacyCode' to 'LegacyCode &'
Run Code Online (Sandbox Code Playgroud)
是否有任何方法可以克服这个问题,而不增加功能膜的可变性?
使LegacyCode::isProcessNameconst:
bool LegacyCode::isProcessName(char* _sName,char* _pName,char* _szProcessName) const;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
54 次 |
| 最近记录: |