应聘C++程序员,他们出来一个MFC的问题,急!!求解,感觉这个又容易有好难,可以发邮箱874615679@qq.com
发布网友
发布时间:2022-05-07 11:36
我来回答
共1个回答
热心网友
时间:2023-10-29 17:06
#include <functional>
#include <list>
#include <algorithm>
#include <utility>
#include <iostream>
using namespace std;
template<typename EventArg>
class EventSource
{
using HandlerType = function<void(EventArg const&)>;
using HandlerList = list<HandlerType>;
public:
using TokenType = void*;
EventSource() = default;
TokenType operator +=(HandlerType handler)
{
m_handlers.push_back(move(handler));
return &m_handlers.back();
}
void operator -=(TokenType t)
{
m_handlers.remove_if([t](HandlerType const& h) { return &h == t; });
}
void operator ()(EventArg const& v)
{
for (auto it = m_handlers.begin(); it != m_handlers.end();)
{
auto cur = it++;
(*cur)(v);
}
}
private:
EventSource(EventSource&) = delete;
EventSource& operator=(EventSource&) = delete;
HandlerList m_handlers;
};
int main()
{
EventSource<int> intEvent;
auto t1 = intEvent += [](int v)
{
cout << "t1: " << v << endl;
};
auto t2 = intEvent += [&](int v)
{
intEvent -= t1;
cout << "t2: " << v << endl;
};
intEvent(2);
intEvent(3);
}
这个实现比较简单,但是可以工作。
热心网友
时间:2023-10-29 17:06
#include <functional>
#include <list>
#include <algorithm>
#include <utility>
#include <iostream>
using namespace std;
template<typename EventArg>
class EventSource
{
using HandlerType = function<void(EventArg const&)>;
using HandlerList = list<HandlerType>;
public:
using TokenType = void*;
EventSource() = default;
TokenType operator +=(HandlerType handler)
{
m_handlers.push_back(move(handler));
return &m_handlers.back();
}
void operator -=(TokenType t)
{
m_handlers.remove_if([t](HandlerType const& h) { return &h == t; });
}
void operator ()(EventArg const& v)
{
for (auto it = m_handlers.begin(); it != m_handlers.end();)
{
auto cur = it++;
(*cur)(v);
}
}
private:
EventSource(EventSource&) = delete;
EventSource& operator=(EventSource&) = delete;
HandlerList m_handlers;
};
int main()
{
EventSource<int> intEvent;
auto t1 = intEvent += [](int v)
{
cout << "t1: " << v << endl;
};
auto t2 = intEvent += [&](int v)
{
intEvent -= t1;
cout << "t2: " << v << endl;
};
intEvent(2);
intEvent(3);
}
这个实现比较简单,但是可以工作。
热心网友
时间:2023-10-29 17:06
#include <functional>
#include <list>
#include <algorithm>
#include <utility>
#include <iostream>
using namespace std;
template<typename EventArg>
class EventSource
{
using HandlerType = function<void(EventArg const&)>;
using HandlerList = list<HandlerType>;
public:
using TokenType = void*;
EventSource() = default;
TokenType operator +=(HandlerType handler)
{
m_handlers.push_back(move(handler));
return &m_handlers.back();
}
void operator -=(TokenType t)
{
m_handlers.remove_if([t](HandlerType const& h) { return &h == t; });
}
void operator ()(EventArg const& v)
{
for (auto it = m_handlers.begin(); it != m_handlers.end();)
{
auto cur = it++;
(*cur)(v);
}
}
private:
EventSource(EventSource&) = delete;
EventSource& operator=(EventSource&) = delete;
HandlerList m_handlers;
};
int main()
{
EventSource<int> intEvent;
auto t1 = intEvent += [](int v)
{
cout << "t1: " << v << endl;
};
auto t2 = intEvent += [&](int v)
{
intEvent -= t1;
cout << "t2: " << v << endl;
};
intEvent(2);
intEvent(3);
}
这个实现比较简单,但是可以工作。
热心网友
时间:2023-10-29 17:06
#include <functional>
#include <list>
#include <algorithm>
#include <utility>
#include <iostream>
using namespace std;
template<typename EventArg>
class EventSource
{
using HandlerType = function<void(EventArg const&)>;
using HandlerList = list<HandlerType>;
public:
using TokenType = void*;
EventSource() = default;
TokenType operator +=(HandlerType handler)
{
m_handlers.push_back(move(handler));
return &m_handlers.back();
}
void operator -=(TokenType t)
{
m_handlers.remove_if([t](HandlerType const& h) { return &h == t; });
}
void operator ()(EventArg const& v)
{
for (auto it = m_handlers.begin(); it != m_handlers.end();)
{
auto cur = it++;
(*cur)(v);
}
}
private:
EventSource(EventSource&) = delete;
EventSource& operator=(EventSource&) = delete;
HandlerList m_handlers;
};
int main()
{
EventSource<int> intEvent;
auto t1 = intEvent += [](int v)
{
cout << "t1: " << v << endl;
};
auto t2 = intEvent += [&](int v)
{
intEvent -= t1;
cout << "t2: " << v << endl;
};
intEvent(2);
intEvent(3);
}
这个实现比较简单,但是可以工作。
热心网友
时间:2023-10-29 17:06
#include <functional>
#include <list>
#include <algorithm>
#include <utility>
#include <iostream>
using namespace std;
template<typename EventArg>
class EventSource
{
using HandlerType = function<void(EventArg const&)>;
using HandlerList = list<HandlerType>;
public:
using TokenType = void*;
EventSource() = default;
TokenType operator +=(HandlerType handler)
{
m_handlers.push_back(move(handler));
return &m_handlers.back();
}
void operator -=(TokenType t)
{
m_handlers.remove_if([t](HandlerType const& h) { return &h == t; });
}
void operator ()(EventArg const& v)
{
for (auto it = m_handlers.begin(); it != m_handlers.end();)
{
auto cur = it++;
(*cur)(v);
}
}
private:
EventSource(EventSource&) = delete;
EventSource& operator=(EventSource&) = delete;
HandlerList m_handlers;
};
int main()
{
EventSource<int> intEvent;
auto t1 = intEvent += [](int v)
{
cout << "t1: " << v << endl;
};
auto t2 = intEvent += [&](int v)
{
intEvent -= t1;
cout << "t2: " << v << endl;
};
intEvent(2);
intEvent(3);
}
这个实现比较简单,但是可以工作。
热心网友
时间:2023-10-29 17:06
#include <functional>
#include <list>
#include <algorithm>
#include <utility>
#include <iostream>
using namespace std;
template<typename EventArg>
class EventSource
{
using HandlerType = function<void(EventArg const&)>;
using HandlerList = list<HandlerType>;
public:
using TokenType = void*;
EventSource() = default;
TokenType operator +=(HandlerType handler)
{
m_handlers.push_back(move(handler));
return &m_handlers.back();
}
void operator -=(TokenType t)
{
m_handlers.remove_if([t](HandlerType const& h) { return &h == t; });
}
void operator ()(EventArg const& v)
{
for (auto it = m_handlers.begin(); it != m_handlers.end();)
{
auto cur = it++;
(*cur)(v);
}
}
private:
EventSource(EventSource&) = delete;
EventSource& operator=(EventSource&) = delete;
HandlerList m_handlers;
};
int main()
{
EventSource<int> intEvent;
auto t1 = intEvent += [](int v)
{
cout << "t1: " << v << endl;
};
auto t2 = intEvent += [&](int v)
{
intEvent -= t1;
cout << "t2: " << v << endl;
};
intEvent(2);
intEvent(3);
}
这个实现比较简单,但是可以工作。
热心网友
时间:2023-10-29 17:06
#include <functional>
#include <list>
#include <algorithm>
#include <utility>
#include <iostream>
using namespace std;
template<typename EventArg>
class EventSource
{
using HandlerType = function<void(EventArg const&)>;
using HandlerList = list<HandlerType>;
public:
using TokenType = void*;
EventSource() = default;
TokenType operator +=(HandlerType handler)
{
m_handlers.push_back(move(handler));
return &m_handlers.back();
}
void operator -=(TokenType t)
{
m_handlers.remove_if([t](HandlerType const& h) { return &h == t; });
}
void operator ()(EventArg const& v)
{
for (auto it = m_handlers.begin(); it != m_handlers.end();)
{
auto cur = it++;
(*cur)(v);
}
}
private:
EventSource(EventSource&) = delete;
EventSource& operator=(EventSource&) = delete;
HandlerList m_handlers;
};
int main()
{
EventSource<int> intEvent;
auto t1 = intEvent += [](int v)
{
cout << "t1: " << v << endl;
};
auto t2 = intEvent += [&](int v)
{
intEvent -= t1;
cout << "t2: " << v << endl;
};
intEvent(2);
intEvent(3);
}
这个实现比较简单,但是可以工作。
热心网友
时间:2023-10-29 17:06
#include <functional>
#include <list>
#include <algorithm>
#include <utility>
#include <iostream>
using namespace std;
template<typename EventArg>
class EventSource
{
using HandlerType = function<void(EventArg const&)>;
using HandlerList = list<HandlerType>;
public:
using TokenType = void*;
EventSource() = default;
TokenType operator +=(HandlerType handler)
{
m_handlers.push_back(move(handler));
return &m_handlers.back();
}
void operator -=(TokenType t)
{
m_handlers.remove_if([t](HandlerType const& h) { return &h == t; });
}
void operator ()(EventArg const& v)
{
for (auto it = m_handlers.begin(); it != m_handlers.end();)
{
auto cur = it++;
(*cur)(v);
}
}
private:
EventSource(EventSource&) = delete;
EventSource& operator=(EventSource&) = delete;
HandlerList m_handlers;
};
int main()
{
EventSource<int> intEvent;
auto t1 = intEvent += [](int v)
{
cout << "t1: " << v << endl;
};
auto t2 = intEvent += [&](int v)
{
intEvent -= t1;
cout << "t2: " << v << endl;
};
intEvent(2);
intEvent(3);
}
这个实现比较简单,但是可以工作。