c++ - 如何在两个不同的单例类或普通类中派生具有不同文件路径的相同函数?

标签 c++

我有两个具有相同函数 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::updateToFileInventoryList 调用该函数::updateToFile.

没有特别需要通过从基类派生来做到这一点,但如果你想那样做,我也没有看到问题。

关于c++ - 如何在两个不同的单例类或普通类中派生具有不同文件路径的相同函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57456583/

相关文章:

c++ - 为什么在创建第二个对象后更改了第一个对象成员

C++ 流日志 Objective-C 对象

c++ - 如何将 LibVLC 中的 D3DDevice 传递为 "HWND"

c++ - 我可以解决 "rebuilding"加载调试器吗?

c++ - 专用于私有(private)成员类的 std::hash

c++ - 将文件中的值添加到 C++ 中的哈希类型结构

C++ - 模板类对象数组

c++ - glGetUniformLocation OpenGL ES 2.0 (ipad 3 iOS 7.0.3 返回结果错误)

c++ - std::shared_ptr<type>(new DerivedType(...)) != std::make_shared<type>(DerivedType(...))?

c++ - C++ 中模板类成员函数的特化