【发布时间】:2019-08-20 09:34:26
【问题描述】:
在下面的代码 sn-p 中,我从 ReadFile() 函数调用 SetParams() 和 Execute() 多次。
我可以通过单个调用优化每个 SetParams() 和 Execute() 吗?
bool SubscriptionRead::ReadFile()
{
IVerification* pReader = new FileReader();
std::wstring oemPathPublicKey(oemFolderPath)
, oemPathSessionKey(oemFolderPath)
, oemPathUserChoices(oemFolderPath);
oemPathPublicKey.append(PUBLIC_KEY_FILE);
oemPathSessionKey.append(SESSION_KEY_FILE);
oemPathUserChoices.append(USERCHOICES_FILE);
pReader->SetParams((wchar_t*)oemPathPublicKey.c_str(), L"file");
pReader->Execute();
pReader->SetParams((wchar_t*)oemPathSessionKey.c_str(), L"file");
pReader->Execute();
pReader->SetParams((wchar_t*)oemPathUserChoices.c_str(), L"file");
pReader->Execute();
return True;
}
void FileReader::SetParams(wchar_t* wszParams, wchar_t* wszParamType)
{
m_wszParamType = wszParamType;
m_wszParams = wszParams;
}
bool FileReader::Execute()
{
if (wcscmp(m_wszParamType, L"registry") == 0)
{
function1();
}
else
{
function2();
}
return true;
}
【问题讨论】:
-
也许可以解释为什么你认为你不能,因为不清楚是什么问题
-
是什么阻碍了您创建另一个在单个调用中执行此操作的函数?
-
将需要的参数直接传递给
Execute(),而不是使用其他函数来设置它们。如果您想将多个Execute()调用合并为一个,请更改参数,使其接受多对参数,或接受数组参数。还可以考虑使用标准库类型(如std::wstring,或者,如果传递一组,std::vector<std::wstring>)而不是使用wchar_t的数组。 -
老实说,我认为这很好。如果您还有更多要添加的内容,请担心。
标签: c++ function class c++11 optimization