Как и @David Pierre, предлагает: find основан на значении: он ищет в диапазоне итераторов указатель (например, 0x0F234420), равный указателю на new Circle(point(1,2),3)
, который вы только что создали. Поскольку это новый объект, его там не будет.
Вы можете обойти это, используя find_if
с оператором, который сравнивает объекты, на которые ссылается указатель.
Однако критерий должен уметь различать типы фигур.
class Shape {
public:
//amongst other functions
virtual bool equal( const Shape* ) const = 0;
};
class Circle : public Shape {
public:
bool equal( const Shape* pOther ) const {
const Circle* pOtherCircle = dynamic_cast<const Circle*>( pOther );
if( pOtherCircle == NULL ) return false;
// compare circle members
}
};
class Rectangle : public Shape {
public:
bool equal( const Shape* pOther ) const {
const Rectangle* pOtherR = dynamic_cast<const Rectangle*>( pOther );
if( pOtherR == NULL ) return false;
// compare rectangle members
}
};
Shape* pFindThis = new Circle(point(1,2),3);
vector<Shape*>::const_iterator itFound = find_if(s1.begin(),s1.end(),
bind1st( mem_fun( &Shape::equal ), pFindThis) ) );
delete pFindThis; //leak resolved by Mark Ransom - tx!
if( itFound != s1.end() ) {
(*itFound)->move(point(10,20));
}