It is sometimes said that interfaces solve several problems of object-oriented programming, in particular the circle-ellipse problem. It would be this interface
class IEllipse {
public:
virtual double GetXRadius() const = 0;
virtual double GetYRadius() const = 0;
virtual void SetXRadius(double r) = 0;
virtual void SetYRadius(double r) = 0;
}
But then we’d still have this kind of problematic function, that works for some implementations of IEllipse, but not for the circle implementation (that plugs both SetXRadius
and SetYRadius
to the same storage variable radius
):
std::string SerializeDestructively(IEllipse& ellipse) {
std::stringstream s;
s << ellipse.GetXRadius() << " ";
ellipse.SetXRadius(0.0);
s << ellipse.GetYRadius();
ellipse.SetYRadius(0.0);
return s.str();
}
It seems the problem is rather that IEllipse
represents a resizable ellispe, in each dimension. The circle does not implement that interface, because when flattened, it is no longer a circle. And writing the definition of an ellipse in an interface does not solve that.