尝试创建异步观察者模式会导致编译器错误 C3867,我不知道如何解决它。示例代码片段如下
class Subject;
class Observer
{
public:
virtual void notify(Subject* s) = 0;
virtual ~Observer() {};
};
class Subject
{
std::map<std::string, Observer *> observers;
protected:
void notify_observers()
{
std::map<std::string, Observer *>::iterator iter;
for (iter = observers.begin(); iter != observers.end(); ++iter) {
void (Observer::*notify)(Subject *) = iter->second->notify;
std::async(std::launch::async, notify, this);
}
}
public:
virtual ~Subject() {};
void observer(std::string id, Observer* o)
{
observers[id] = o;
}
};
template<typename Iter, typename type>
class Sort : public Observer {
public:
virtual void notify(Subject* s)
{
TestSort<Iter> *a;
a = dynamic_cast<TestSort<Iter> *>(s);
std::vector<type> temp(a->beg(), a->end());
sort(temp->beg(), temp->end());
}
};
template<typename Iter, typename type>
class InsertionSort : public Sort<Iter, type>
{
void sort(Iter beg, Iter end) {
for (Iter i = beg; i != end; ++i)
std::rotate(std::upper_bound(beg, i, *i), i, i+1);
}
};
int main ()
{
std::vector<double> data(100);
std::generate(data.begin(), data.end(), [](){return rand() % 500;} );
auto ts = TestSort<std::vector<double>::iterator >(data.begin(), data.end());
auto is = new InsertionSort<std::vector<double>::iterator, double >();
//.................
ts.observer("InsertionSort", is);
//.........................
ts.Triggerd();
return 0;
}
虽然我理解错误
error C3867: 'Observer::notify': function call missing argument list; use '&Observer::notify' to create a pointer to member
然而在这种情况下,我无法弄清楚如何解决它。
在这种情况下,如果 notify 将是一个简单的可添加成员函数,而不是
void (Observer::*notify)(Subject *) = iter->second->notify;
我本可以简单地写
void (Observer::*notify)(Subject *) = &Observer::notify;
但是 notify 是一个多态函数,我无法在编译时处理正确的函数。
请建议我应该如何处理