c++ - Comparing pointers of object with no data member -
i have following subject-observor design
template<typename data> struct observor{ observor() { } virtual void process(data data) = 0; }; template <typename data> class subject { public: void addobservor(observor<data>* observor) { _observors.insert(observor); } void removeobservor(observor<data>* observor) { boost::unordered_set<observor<data> * > ::iterator itr = _observors.find(observor); if(itr != _observors.end() ) { _observors.erase(observor); } } protected: boost::unordered_set<observor<data>* > _observors; };
in code above, observor class inherited classes wish register subject. in function removeobservor, compile error when try create iterator. solution suggested me implement comparator (i.e. <). observor class has no data members. base comparator on?
the solution came mind use typeid of each observor derived class(i.e derived1, derived2). solution works if there exists 1 object of each derived class in set. since may have multiple instances of same object type, i'm stuck on how proceed
i'm guessing error iterator
dependent typename. add typename
front of iterator variable delcaration, or better use auto
.
eg,
typename boost::unordered_set<observor<data> * > ::iterator itr = _observors.find(observor);
or
auto itr = _observors.find(observor);
Comments
Post a Comment