There are two classes: Point , Pixel . Pixel derived from Point .
There is an array of pointers Point* , which stores pointers to objects of type Point and Pixel .
You need to sort the array like this:
- When comparing
PointandPoint- comparing the results ofp1.dist()andp2.dist() - When comparing
PointandPixel- comparing the results ofp1.dist()andp2.dist() - When comparing
PixelandPoint- comparing the results ofp1.dist()andp2.dist() - When comparing
PixelandPixel- comparing the results ofp1.dist()andp2.dist(). If they are the same, then compare the color (string) of two pixels.
My decision:
In the Pixel and Point classes, there are less overloaded virtual functions (class methods) that are accepted by the parameter const Pixel & and const Point & , respectively.
Problem:
Pointer dereferencing of Point does not return a dynamic type, but const Point & , therefore when called:
Point* p1=new Point; Point* p2=new Pixel; return *p1<*p2; For the operand p1 , the function less is called from the class of the corresponding dynamic type p1 due to the mechanism of polymorphism. But regardless of the dynamic type of p2 , the overloaded bool less (const Point &) function is always called, instead of bool less (const Pixel &)
I will explain a little:
*p1<*p2; //Point::less(const Point&) вместо Point::less(const Pixel&) *p2<*p1; //Pixel::less(const Point&) *p1<*p1; //Point::less(const Point&) *p2<*p2; //Pixel::less(const Point&) вместо Pixel::less(const Pixel&)
dist()is virtual? If not, can it be made virtual? Just if it is virtual, compare the result of its call through a pointer to aPointand do not suffer ... - Harry