boost::ptr_vector and find_if

后端 未结 3 1957
心在旅途
心在旅途 2021-01-28 18:53

I have a class:

//header file
class CMDatabase
{
    class Try;
    typedef boost::shared_ptr TryPtr;
    typedef boost::ptr_vector TryVect         


        
3条回答
  •  面向向阳花
    2021-01-28 19:30

    The issue is that your equal method is not const qualified.

    class Try
    {
    public:
      virtual ~Try();
      virtual bool equal(CMDatabase::TryPtr const& mySd) const = 0;
    };
    
    bool TryImpl::equal(CMDatabase::TryPtr const& mySd) const { return true; }
    

    Note:

    • the const added to the method, otherwise it cannot be used on const objects
    • the const added on the pointer: copying shared_ptr does cost, because it necessitates incremented a shared counter, and decrementing it afterward.

    EDIT:

    Reminder for the unwary: the Pointer Container library has been designed so that the interface would be as easy to use a possible, and one of the goody is that you don't have to double dereference. This compiles:

     boost::ptr_vector vec;
     vec.push_back(new int(3));
    
     int& i = *vec.begin();
    

    Thus, your functor must take a reference, not a pointer :)

提交回复
热议问题