Skip to main content
gamedev.net
🔒 Locked

overloading operators c++

Started by nikita 3 replies 900+ views
Original Post
nikita
nikita
hy, i use a class and i overload the <-operator for this class. the problem is the following: i've to use a priority_queue (stl) and this priority_queue should handle with pointers (to the class), how can i overload the <-operator that the priority_queue-template compares the classes (not not just the pointer-adresses)?
-- Nietzsche ist tot --
snk_kid
snk_kid
Define a custom comparator function or functor:

struct foo {   //....   bool operator<(const foo& f) const { .... }};inline bool compare_foos(const foo* const lhs,                         const foo* const rhs) {   return *lhs < *rhs;}....typedef std::priority_queue< foo*,                              std::vector<foo*>,                             compare_foos > foo_queue;
Couvillion
Couvillion
Quote:
Original post by snk_kid
Define a custom comparator function or functor:

struct foo {   //....   bool operator<(const foo& f) const { .... }};inline bool compare_foos(const foo* const lhs,                         const foo* const rhs) {   return *lhs < *rhs;}....typedef std::priority_queue< foo*,                              std::vector<foo*>,                             compare_foos > foo_queue;


Or, if you're really slick, you can generalize it:

template<class T, class Compare = std::less<T> > struct pointeeCompare{  bool operator()(T* lhs, T* rhs)  {     return Compare()(*lhs, *rhs);  }};....typedef std::priority_queue< foo*,                              std::vector<foo*>,                             pointeeCompare<foo> > foo_queue;
~~C--O -
Fruny
Fruny
Quote:
Original post by Couvillion
Or, if you're really slick, you can generalize it


And if you're really, really slick, you'll make it an adaptable functor. [grin]

template<class T, class Compare = std::less<T> > struct pointeeCompare :   std::binary_function<T,T,bool>{  ...}
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
snk_kid
snk_kid
Quote:
Original post by Couvillion
Or, if you're really slick, you can generalize it:

template<class T, class Compare = std::less<T> > struct pointeeCompare{  bool operator()(T* lhs, T* rhs)  {     return Compare()(*lhs, *rhs);  }};....typedef std::priority_queue< foo*,                              std::vector<foo*>,                             pointeeCompare<foo> > foo_queue;


Not quite, that code will bork if you pass the address of a free-function there-for its not generalized that much and what happened to constant correctness [grin]

By the way the reason why i showed only a simple function was i'm assuming that he probably has never come across things like functors before otherwise he would have never asked that question in the first place.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.