Question:
and again good evening to all members of the forum. There was a problem with redefining std :: function <void ()> objects, and as if the thing is not particularly difficult, but it’s impossible to figure it out. I have some class:
введите код здесь
#include <functional>
class ProcessEventHandler
{
public:
std::function<void()> ProcessCrashHandler;
std::function<void()> ProcessStopHandler;
};
with its help I will implement Kolbek in the next class
class ProcessManager : public ProcessEventHandler
{
public:
ProcessManager(const std::string &sFilePath);
ProcessManager(const _int32 &ProcessID);
~ProcessManager();
............
private:
void onProcessStart();
void onProcessWatch();
void onProcessCrash();
void onProcessStopped();
void onProcessManulyStop();
...........
};
the problem is that for objects
std::function<void()> ProcessCrashHandler;
std::function<void()> ProcessStopHandler;
in the class, the descendant needs to be assigned functions
void onProcessCrash();
void onProcessStopped();
trying to do this in the constructor
ProcessManager::ProcessManager(const std::string &sFilePath) :
m_ppiProcInfo(),
m_sCommandLine(sFilePath),
m_isRunning(false)
{
ProcessCrashHandler = onProcessCrash;
ProcessStopHandler = &ProcessManager::onProcessStopped;
m_pcEventLoger = new EventLogger(getCurrentDirPath());
onProcessStart();
}
As soon as I didn’t try anything, it didn’t work, I tried it through the lambda, it didn’t work either. I can’t understand whether my hands are crooked, or the compiler is out of order Help please
mistakes
error C2679: binary '=' : no operator found which takes a right-hand operand of type 'overloaded-function' (or there is no acceptable conversion)
error C3867: 'ProcessManager::onProcessCrash': function call missing argument list; use '&ProcessManager::onProcessCrash' to create a pointer to member
Answer:
No, no, that won't work.
The point is that onProcessCrash
is not a function! Therefore, you cannot store it not only in a variable of type std::function<void(void)>
, but even in an ordinary pointer to a function of type void (*)(void)
.
This is actually a member function, you need a specific this
to call it. Binding this
is easy: using std::bind
.
Try this:
ProcessCrashHandler = std::bind(&ProcessManager::onProcessCrash, this);