I have the following operator overloading functions for a class named cRoom. The constructor of this class simply sets default values for some of it's data members.
When, in main(), I wote the following expression, I was expecting an error with my compiler:Code:bool operator >(const cRoom& aRoom) const { return (this->area() > aRoom.area()); } //bool operator >(const double& value) const { return (this->area() > value); }
if(oKitchen > 127.0) { //... do something}
The fact is that not only I had no error, but it also worked perfectly.
I found it strange and proceed to step into my code. What I found amazing was that the expression above, actually called the constructor of the cRoom class, passed 127.0 to the first argument, did the operator overloading and then called the destructor.
How come an object was created when the only thing that happens in the right side of the > sign is a constant?
More... I did the following and it behaved the exact same way:
float a = 127.0;
if(oKitchen > a) { //... do something}
When I removed the comment from the second operator overload, it behaved as expected and proceed without creating an object. But why did the first form worked?