I have a Rectangle
class with conversion operators to both double
and std::string
:
class Rectangle
{
public:
Rectangle(double x, double y) : _x(x), _y(y) {}
operator std::string ();
operator double ();
private:
double _x, _y;
double getArea() {return _x * _y;}
};
int main()
{
Rectangle r(3, 2.5);
cout << r << endl;
return 0;
}
I don’t understand why operator double()
is invoked, rather than operator std::string()
.
As far as I know, according to C++ wikibook,
operator double
is used to convert Rectangle
objects to double
.
So what's going on here? Is it related to the fact that an int
is passed to the constructor? If so, why?