Is it dangerous to overload bool operator in this case

boolean, c++, casting, operator-overloading

Solution

That is not good enough. Since you're stuck with C++03, safe-bool idiom is what you should be using if you need such a thing at all (otherwise, `explicit` conversion function should be preferred).

An alternative solution is to return `boost:optional` instead:

boost::optional<point> intersect(const line& a, const line& b);

I would adopt this approach (even in C++11), as it looks semantically better — parallel lines don't have intersection point, so the return value should indeed be optional (or maybe value).

Problem

I have seen comments or answers on SoF stating that overloading the cast operator to bool is dangerous and I should prefer the `void* operator` instead. Still I would like to ask if it is dangerous practice to use this operator in my use case and if yes why. I implement a simply geometry library and one of its most basic classes is a point that I define in the following way: ``` struct point { double x, y; bool real; point(double x_, double y_): x(x_), y(y_), real(true) {} point(): x(0), y(0), real(true) {} operator bool() { return real; } }; ``` I will try to explain why I need the cast operator to bool. I have another class called line and I have a function declared as follows: ``` point intersect(const line& a, const line& b); ``` Now having overloaded the cast operator to bool of a point I can write both: `point p = intersect(a,b);` and `if (intersect(a,b))` thus either getting the intersection point of two lines or checking if two lines intersect. I use this in other places as well but I believe this example is enough to show the usage of the cast operator. Is my usage of the cast operator dangerous and if it is could you please give an example when the effect will not be as expected? EDIT: one small addition thanks to juanchopanza : in this project I am limited to not using c++11, so I can not make the operator explicit.

Original source

Related problems