Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/157.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在两个不同的单例类或普通类中派生具有不同文件路径的相同函数?_C++ - Fatal编程技术网

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

C++ 如何在两个不同的单例类或普通类中派生具有不同文件路径的相同函数?,c++,C++,我有两个具有相同函数uploadToFile()的不同类,它们采用不同的文件路径 我现在已经使用了singleton类,并且有不同的对象来访问该函数 代码如下 Inventorylist.cpp InventoryList* InventoryList::pInventoryList =NULL; std::once_flag InventoryList::InventoryListInstanceFlag; InventoryList* InventoryList::getInstance(

我有两个具有相同函数uploadToFile()的不同类,它们采用不同的文件路径

我现在已经使用了singleton类,并且有不同的对象来访问该函数

代码如下

Inventorylist.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);
    }
}
InventoryList*InventoryList::pInventoryList=NULL;
std::once_标志InventoryList::InventoryListInstanceFlag;
InventoryList*InventoryList::getInstance()
{
std::调用_一次(InventoryListInstanceFlag,[]{
pInventoryList=new InventoryList();
pInventoryList->runInternalThread=true;
pthread_create(&pInventoryList->writer,NULL,(void*(*)(void*)&InventoryList::updateToFile,pInventoryList);
}
);
返回pInventoryList;
}
节点列表
NodeList*NodeList::pNodeList=NULL;
std::once_标志NodeList::NodeListInstanceFlag;
NodeList*NodeList::getInstance()
{
std::调用_一次(NodeListInstanceFlag,[]{
pNodeList=新节点列表();
pNodeList->runInternalThread=true;
pthread_创建(&pNodeList->writer,NULL,(void*(*)(void*))&NodeList::updateToFile,pNodeList);
}
);
返回pNodeList;
}
InventoryList::UpdateTofile()
void InventoryList::updateToFile()
{
while(true)
{
if((pInventoryList->writeTImer.GetEnabled()&&pInventoryList->writeTImer.IsDone())||
pInventoryList->timer.IsDone()
{
std::unique_lock invlock(pInventoryList->mutex_NodeInvConf);
//#如果调试日志
文件*pFile;
std::string conff=pInventoryList->NodeInvConfiguration.toStyledString();
//日志(“库存文件数据%s”,conff.c_str());
pFile=fopen(KV_文件夹\位置目录\ SEP KV_可读\清单\文件,“wb”);
fwrite(conff.c_str(),sizeof(char),conff.length(),pFile);
fclose(pFile);
sync();
//#恩迪夫
文件*pFile2;
pFile2=fopen(/*KV\U文件夹\U位置目录\U SEP KV\U库存清单\U文件*/SYS\U库存路径,“wb”);
PrintJSONTree(pInventoryList->NodeInvConfiguration,0,pFile2);
fclose(pFile2);
sync();
pInventoryList->writeTImer.SetEnabled(false);
日志(“更新的库存文件”);
日志信息(“库存文件更新”);
MessageQueInterface::getInstance()->sendInventory(true);
pInventoryList->timer.Reset();
}
睡眠(1);
}
}
NodeList::updateToFile()
void NodeList::updateToFile()
{
while(true)
{
if(pNodeList->writeTImer.GetEnabled()&&pNodeList->writeTImer.IsDone())
{
std::unique_lock panellock(pNodeList->mutex_NodeInvConf);
//#如果调试日志
文件*pFile;
std::string conff=pNodeList->NodeInvConfiguration.toStyledString();
pFile=fopen(KV_文件夹\位置目录\ SEP KV_可读\节点列表文件,“wb”);
fwrite(conff.c_str(),sizeof(char),conff.length(),pFile);
fclose(pFile);
sync();
//#恩迪夫
文件*pFile2;
pFile2=fopen(/*KV_文件夹_位置目录_SEP KV_节点列表_文件*/SYS_PNL_路径,“wb”);
PrintJSONTree(pNodeList->NodeInvConfiguration,0,pFile2);
fclose(pFile2);
sync();
pNodeList->writeTImer.SetEnabled(false);
日志(“节点列表文件更新”);
日志信息(“节点列表文件更新”);
MessageQueInterface::getInstance()->sendInventory(false);
}
睡眠(2);
}
}
我想在一个基类中编写这两个不同的updateTofile,并通过传递文件路径派生到两个不同的类

有人能告诉我怎么做吗


谢谢

只需创建一个以文件名为参数的全局函数
updateToGiveFile
(或其他任何函数),然后从
NodeList::updateToFile
InventoryList::updateToFile
调用该函数


没有必要通过从基类派生来实现这一点,但如果您想这样做,我也不认为有什么问题。

只需创建一个全局函数
updateToGivenFile
(或其他任何函数),该函数以文件名为参数,并从
NodeList::updateToFile
InventoryList::updateToFile
调用该函数


没有特别需要通过从基类派生来实现这一点,但是如果您想这样做,我也不认为有什么问题。

应该相当简单。创建基类:

class BaseList
{
public:
  BaseList(const std::string& path)
  : path( path ), thread(&BaseList::updateToFile, this)
  {
  }

private:
  std::string path;
  std::thread thread;

  void updateToFile();
};
请注意,我已将
pthread
的用法替换为
std::thread
,这使您的代码可移植且更简单

然后可以创建子类:

class NodeList : public BaseList
{
public:
  static NodeList& getInstance()
  {
    static NodeList instance;
    return instance;
  }
private:
  NodeList()
  : BaseList("Nodes path")
  {}
};

class InventoryList: public BaseList
{
public:
  static InventoryList& getInstance()
  {
    static InventoryListinstance;
    return instance;
  }
private:
  InventoryList()
  : BaseList("Inventory path")
  {}
};

在这里,我用更简单的函数静态替换了std::call_once,以创建实例。

它应该相当简单。创建基类:

class BaseList
{
public:
  BaseList(const std::string& path)
  : path( path ), thread(&BaseList::updateToFile, this)
  {
  }

private:
  std::string path;
  std::thread thread;

  void updateToFile();
};
请注意,我已将
pthread
的用法替换为
std::thread
,这使您的代码可移植且更简单

然后可以创建子类:

class NodeList : public BaseList
{
public:
  static NodeList& getInstance()
  {
    static NodeList instance;
    return instance;
  }
private:
  NodeList()
  : BaseList("Nodes path")
  {}
};

class InventoryList: public BaseList
{
public:
  static InventoryList& getInstance()
  {
    static InventoryListinstance;
    return instance;
  }
private:
  InventoryList()
  : BaseList("Inventory path")
  {}
};

在这里,我用更简单的函数静态替换了std::call_once,以创建实例。

C++作为自由函数的概念。所以在大多数情况下,您不想使用继承,而是创建自由函数

而你的
updateToFile
无疑是一个免费函数的候选者:

void write_to_文件(节点列表*pNodeList)
{
while(true)
{
if(pNodeList->writeTImer.GetEnabled()&&pNodeList->writeTImer.IsDone())
{
std::unique_lock panellock(pNodeList->mutex_NodeInvConf);
//#如果调试日志
文件*pFile;
std::string conff=pNodeList->NodeInvConfiguration.toStyledString();
pFile=fopen(KV_文件夹\位置目录\ SEP KV_可读\节点列表文件,“wb”);
fwrite(conff.c_str(),sizeof(char),conff.length(