我有两个具有相同函数 uploadToFile() 的不同类,它们采用不同的文件路径。
我现在已经使用了单例类,并且有不同的对象来访问该函数。
代码如下。
库存 list .cpp
InventoryList* InventoryList::pInventoryList =NULL;
std::once_flag InventoryList::InventoryListInstanceFlag;
InventoryList* InventoryList::getInstance()
{
std::call_once(InventoryListInstanceFlag, []{
pInventoryList = new InventoryList();
pInventoryList->runInternalThread=true;
pthread_create(&pInventoryList->writer, NULL, (void* (*)(void*))&InventoryList::updateToFile, pInventoryList);
}
);
return pInventoryList;
}
NodeList.cpp
NodeList* NodeList::pNodeList =NULL;
std::once_flag NodeList::NodeListInstanceFlag;
NodeList* NodeList::getInstance()
{
std::call_once(NodeListInstanceFlag, []{
pNodeList = new NodeList();
pNodeList->runInternalThread=true;
pthread_create(&pNodeList->writer, NULL, (void* (*)(void*))&NodeList::updateToFile, pNodeList);
}
);
return pNodeList;
}
InventoryList::UpdateTofile()
void InventoryList::updateToFile()
{
while(true)
{
if((pInventoryList->writeTImer.GetEnabled() && pInventoryList->writeTImer.IsDone()) ||
pInventoryList->bailoutTimer.IsDone())
{
std::unique_lock<std::recursive_mutex> invlock(pInventoryList->mutex_NodeInvConf);
//#if _DEBUG_LOG
FILE * pFile;
std::string conff = pInventoryList->NodeInvConfiguration.toStyledString();
// LOG_G("inventory file data %s",conff.c_str());
pFile = fopen (KV_FOLDER_LOCATION DIR_SEP KV_READABLE_INVENTORYLIST_FILE, "wb");
fwrite (conff.c_str() , sizeof(char), conff.length(), pFile);
fclose (pFile);
sync();
//#endif
FILE * pFile2;
pFile2 = fopen (/*KV_FOLDER_LOCATION DIR_SEP KV_INVENTORYLIST_FILE*/SYS_INV_PATH , "wb");
PrintJSONTree(pInventoryList->NodeInvConfiguration,0,pFile2);
fclose (pFile2);
sync();
pInventoryList->writeTImer.SetEnabled(false);
LOG_G("Inventory file updated");
LOGS_INFO("Inventory file updated");
MessageQueInterface::getInstance()->sendInventory(true);
pInventoryList->bailoutTimer.Reset();
}
sleep(1);
}
}
NodeList::updateToFile()
void NodeList::updateToFile()
{
while(true)
{
if(pNodeList->writeTImer.GetEnabled() && pNodeList->writeTImer.IsDone())
{
std::unique_lock<std::recursive_mutex> panellock(pNodeList->mutex_NodeInvConf);
//#if _DEBUG_LOG
FILE * pFile;
std::string conff = pNodeList->NodeInvConfiguration.toStyledString();
pFile = fopen (KV_FOLDER_LOCATION DIR_SEP KV_READABLE_NODELIST_FILE , "wb");
fwrite (conff.c_str() , sizeof(char), conff.length(), pFile);
fclose (pFile);
sync();
//#endif
FILE * pFile2;
pFile2 = fopen (/*KV_FOLDER_LOCATION DIR_SEP KV_NODELIST_FILE*/SYS_PNL_PATH, "wb");
PrintJSONTree(pNodeList->NodeInvConfiguration,0,pFile2);
fclose (pFile2);
sync();
pNodeList->writeTImer.SetEnabled(false);
LOG_G("Nodelist file updated");
LOGS_INFO("Nodelist file updated");
MessageQueInterface::getInstance()->sendInventory(false);
}
sleep(2);
}
}
我想在一个基类中编写这两个不同的 updateTofile 并通过传递文件路径派生到两个不同的类。
谁能建议我怎么做?
谢谢
最佳答案
只需创建一个将文件名作为参数的全局函数 updateToGivenFile
(或其他),然后从 NodeList::updateToFile
和 InventoryList 调用该函数::updateToFile
.
没有特别需要通过从基类派生来做到这一点,但如果你想那样做,我也没有看到问题。
关于c++ - 如何在两个不同的单例类或普通类中派生具有不同文件路径的相同函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57456583/