Mam taki kod:
#include <iostream>
const float pi = 3.14159;
class Porownalne
{
public:
virtual bool operator<(Porownalne& d) const =0;
};
class Shape: public Porownalne
{
public:
virtual float field()
{
return -1.0;
}
bool operator<(Porownalne& d) const //Ta metoda w ogóle sie nie wywołuje
{
Shape& k = dynamic_cast<Shape&>(d);
return true;
}
};
class Square: public Shape
{
public:
Square(const float side): a(side){}
float field()
{
return a * a;
}
private:
float a;
};
class Circle: public Shape
{
public:
Circle(const float diameter): a(diameter){}
float field()
{
return pi * a;
}
private:
float a;
};
int main ()
{
Shape* patternShape = new Circle(3);
Shape* shape = new Square(3);
if (patternShape < shape)
std::cout<<"wzorcowy ksztalt jest mniejszy\n";
else
std::cout<<"wzorcowy ksztalt nie jest mniejszy\n";
}
Nie wiem dlaczego nie wywołuje mi funkcji bool operator<(Porownalne& d) const
, a także jak w niej zaimplementować porównywanie:
patternShape < shape
w taki sposób, aby było porównywane patternShape->field() < shape->field()
?