Operator< overloading

Discussion in 'C++' started by Bas Nedermeijer, Aug 16, 2006.

  1. Hello,

    i am having trouble to use the sort() function of a list<>.

    I cannot seem to overload the operator<().
    The variables to compare are references, like

    Item* itemA;
    Item* itemB;

    when comparing "itemA < itemB" my overloaded function isnt called. When
    the variables are defined;

    Item itemA;
    Item itemB;

    the overloaded function is called.

    I hope it is possible for what i want.


    Below is my "operator<", also a "const diritem& rhs" didnt resolve my
    problem.
    int diritem::eek:perator<(const diritem* rhs) const {

    printf("%s < %s\n",this->_filename.c_str(), rhs->_filename.c_str());

    if ((this->_type==DT_DIR) && (rhs->_type!=DT_DIR)) {
    return 1;
    }
    if ((this->_type!=DT_DIR) && (rhs->_type==DT_DIR)) {
    return 0;
    }

    return (this->_filename < rhs->_filename);

    }

    Any pointers are welcome!

    Kind regards,
    Bas Nedermeijer
    Bas Nedermeijer, Aug 16, 2006
    #1
    1. Advertising

  2. Bas Nedermeijer

    Guest

    If you make the operator argument "const &" again, you should be
    able to call the operator with;

    *ptr < *ptr

    in case of pointers.

    Hope this helps.

    Tolga Ceylan
    , Aug 16, 2006
    #2
    1. Advertising

  3. wrote:
    > If you make the operator argument "const &" again, you should be
    > able to call the operator with;
    >
    > *ptr < *ptr
    >
    > in case of pointers.


    I cannot change the sorting routine, because i want to make use of the
    built-in sorting routing of list<>.

    But thanks for the reply,

    Kind regards,
    Bas Nedermeijer
    Bas Nedermeijer, Aug 16, 2006
    #3
  4. Bas Nedermeijer a écrit :
    > wrote:
    >> If you make the operator argument "const &" again, you should be
    >> able to call the operator with;
    >>
    >> *ptr < *ptr
    >>
    >> in case of pointers.

    >
    > I cannot change the sorting routine, because i want to make use of the
    > built-in sorting routing of list<>.
    >
    > But thanks for the reply,
    >
    > Kind regards,
    > Bas Nedermeijer


    Hi ! Your problem is that you cannot overload the < operator for
    pointers, as it already exists (it compare the actual address of the
    pointers). You need either to create a list of objects (and not
    pointers), or to use smart pointers. In the case of smart pointers, you
    will be able to redefine the < operator for them. However, why do you
    use lists of pointers ? This is very dangerous as the list will never
    take care of freeing memory before removing elements.

    Pierre
    Pierre Barbier de Reuille, Aug 16, 2006
    #4
  5. Bas Nedermeijer

    Alan Johnson Guest

    Bas Nedermeijer wrote:
    > Hello,
    >
    > i am having trouble to use the sort() function of a list<>.
    >
    > I cannot seem to overload the operator<().
    > The variables to compare are references, like
    >
    > Item* itemA;
    > Item* itemB;
    >
    > when comparing "itemA < itemB" my overloaded function isnt called. When
    > the variables are defined;
    >
    > Item itemA;
    > Item itemB;
    >
    > the overloaded function is called.
    >
    > I hope it is possible for what i want.
    >
    >
    > Below is my "operator<", also a "const diritem& rhs" didnt resolve my
    > problem.
    > int diritem::eek:perator<(const diritem* rhs) const {
    >
    > printf("%s < %s\n",this->_filename.c_str(), rhs->_filename.c_str());
    >
    > if ((this->_type==DT_DIR) && (rhs->_type!=DT_DIR)) {
    > return 1;
    > }
    > if ((this->_type!=DT_DIR) && (rhs->_type==DT_DIR)) {
    > return 0;
    > }
    >
    > return (this->_filename < rhs->_filename);
    >
    > }
    >
    > Any pointers are welcome!
    >
    > Kind regards,
    > Bas Nedermeijer


    The list container has a sort member template that takes a binary
    predicate to use for sorting. What you need to do is pass in a
    predicate that sorts the objects based on pointers to those objects.
    The following template may help:

    template <typename T>
    struct ptr_less
    {
    bool operator()(T * p1, T * p2)
    {
    return *p1 < *p2 ;
    }
    } ;


    And, for what it is worth, here is the program I used to test that
    template:

    template <typename T>
    struct ptr_less
    {
    bool operator()(T * p1, T * p2)
    {
    return *p1 < *p2 ;
    }
    } ;

    #include <list>
    #include <iostream>
    #include <algorithm>

    void ptr_out(int * p)
    {
    std::cout << *p << ' ' ;
    }

    int main()
    {
    int a[] = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 } ;

    // Add pointers to the list.
    std::list<int *> v ;
    for (std::size_t i = 0; i < sizeof(a)/sizeof(a[0]); ++i)
    v.push_back(a+i) ;

    // Show the list unsorted.
    std::for_each(v.begin(), v.end(), ptr_out) ;
    std::cout << std::endl ;

    // Sort the list.
    v.sort(ptr_less<int>()) ;

    // Show the list sorted.
    std::for_each(v.begin(), v.end(), ptr_out) ;
    std::cout << std::endl ;
    }

    --
    Alan Johnson
    Alan Johnson, Aug 16, 2006
    #5
  6. Bas Nedermeijer

    Howard Guest

    "Pierre Barbier de Reuille" <> wrote in message
    news:44e36a71>

    > However, why do you use lists of pointers ? This is very dangerous as the
    > list will never take care of freeing memory before removing elements.
    >


    I can't answer for the OP, but a quite common reason to store pointers is
    when you want polymorphism. Storing base class pointers allows you to store
    descendant types in the container.

    -Howard
    Howard, Aug 16, 2006
    #6
  7. Bas Nedermeijer

    Kai-Uwe Bux Guest

    Bas Nedermeijer wrote:

    > wrote:
    >> If you make the operator argument "const &" again, you should be
    >> able to call the operator with;
    >>
    >> *ptr < *ptr
    >>
    >> in case of pointers.

    >
    > I cannot change the sorting routine, because i want to make use of the
    > built-in sorting routing of list<>.


    No problem: just define a custom comparison function

    bool compare_item_ptr ( item*, item* ) {
    // whatever
    }

    and do

    list<item*> l;

    ...

    l.sort( compare_item_ptr );


    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, Aug 16, 2006
    #7
  8. Alan Johnson wrote:
    > Bas Nedermeijer wrote:

    <snip>


    > The list container has a sort member template that takes a binary
    > predicate to use for sorting. What you need to do is pass in a
    > predicate that sorts the objects based on pointers to those objects.
    > The following template may help:
    >

    <snip>

    Thanks for your solution, it works!

    - Bas Nedermeijer
    Bas Nedermeijer, Aug 17, 2006
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Smith
    Replies:
    2
    Views:
    410
    Ivan Vecerina
    Oct 6, 2004
  2. Replies:
    11
    Views:
    715
    James Kanze
    May 16, 2007
  3. hurcan solter
    Replies:
    3
    Views:
    712
    Cholo Lennon
    Aug 29, 2007
  4. Replies:
    11
    Views:
    544
  5. Replies:
    2
    Views:
    299
Loading...

Share This Page