Boost logo

Boost Users :

Subject: Re: [Boost-users] shared_ptr to object in stl set container?
From: Nathan Ridge (zeratul976_at_[hidden])
Date: 2011-12-13 05:06:10


> Your operators are likely not being called because they are of the
> incorrect signature. The type of a member equality operator should be
> as shown:
> struct Foo
> {
> bool operator==(const Foo& f) const;
>
> // this is the incorrect equivalent of your operator
> // bool operator==(Foo f);
> }
>
>
> The const parameter is the correct way to do it, but it is most likely
> the method not being const that prevents your operators from being
> invoked.

Actually, the reason the operator< is not being invoked is because
the way it's defined, it compares a Node object to a NodePtr:

class Node
{
  ... 
  bool operator<(NodePtr otherNode) {
    return (id < otherNode->id);
  }
};

What you want is an operator< that compares a NodePtr to a NodePtr,
because this is what set<NodePtr>::find() will call. This needs to be
defined as a non-member function:

bool operator<(NodePtr a, NodePtr b)
{
    return a->getID() < b->getID();
}

Having said that, Rob is right that using ptr_container is a better approach.

Regards,
Nate
                                               


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net