Home > Back-end >  C with class management STD: : function sets the callback to call//interface function parameters how
C with class management STD: : function sets the callback to call//interface function parameters how

Time:11-09

Vs2015 code compilation but below.

 
#include
#include
#include
using namespace std;

Struct stu {
Int a, {1}.
Int b {2};
};

Typedef from & lt; Stu> StuMsg

The class test {
Public:
Void Minus (stuMsg& MSG)
{
Int res=MSG. B - MSG. A;
}
};


The class msgSet {
Public:
Void setMsg (int num, STD: : function & ParMsg)
{
_mapMsg. Insert (STD: : pair &> (num, parMsg));
}
Void callMsg (int num, stuMsg& MSG)
{
STD: : map &> : : iterator it=_mapMsg. Find (num);
If (it!=_mapMsg. End ())
{
It - & gt; The second (MSG);
}
}
Private:
STD: : map &> _mapMsg;
};


Int main ()
{
The test testObj;
MsgSet msgSetObj;
MsgSetObj. SetMsg (0, STD: : bind (& amp; The test: : Minus, & amp; TestObj, STD: : placeholders: : _1));
();
MsgSetObj. CallMsg (0, stuMsgData);
}

CodePudding user response:

 
using namespace std;

Struct stu {
Int a, {1}.
Int b {2};
};

Typedef STD: : from & lt; Stu> StuMsg;

The class test {
Public:
Void Minus (stuMsg& MSG)
{
Int res=MSG - & gt; B - MSG - & gt; a;
}
};


The class msgSet {
Using fun_type=void (stuMsg&);
Public:
Void setMsg (int num, STD: : function{
_mapMsg. Insert (STD: : make_pair (num, parMsg));
}
Void callMsg (int num, stuMsg& MSG)
{
Auto iter=_mapMsg. Find (num);
If (_mapMsg end ()!=iter & amp; & Nullptr!=iter - & gt; Second)
{
Iter - & gt; The second (MSG);
}
}
Private:
STD: : map _mapMsg;
};


Int main ()
{
The test testObj;
MsgSet msgSetObj;
MsgSetObj. SetMsg (0, STD: : bind (& amp; The test: : Minus, & amp; TestObj, STD: : placeholders: : _1));
StuMsg stuMsgData=https://bbs.csdn.net/topics/std::make_shared ();
MsgSetObj. CallMsg (0, stuMsgData);

return 0;
}
  • Related