can any one explain why int is overloaded and my doubt how we consider int as operator ??
and more thing when i trying to overload the float in the same code compiler is showing this error "more than one conversion function from "CB" to a built-in type" ..
The operator int() allows implicit and explicit casting to ints. This means that when the CB functionality cannot be applied, it tries if it works for an int (implicit) and you can directly cast a CB object to an int value (by using int(x), (int)x, dynamic_cast<int>(x) or static_cast<int>(x)).
operator int means that you can convert an object of your class to an integer. you can have such operators for any type. They can often lead to errors though. They occur when compiler can't decide which conversion to use. too slow..
Because in C++, adding variables of type char and float and int and double to each other is implicit and legal.
So for the compiler to do do this: obj2 = obj1 + 3;
the compiler can use either operator float() or operator int() so
it complains about the ambiguity.
the same goes for this statement: obj2 = obj1 + 5.0
...which drives me nuts, because the compiler should be smart enough to know that if you write "5.0" that is a floating-point (because it has a big old decimal point in it), and that "3" is an integer (because it does not have a honkin' decimal point).
If the operation of the routines is so different between int and float then there is a problem with the code, not the compiler.
I think that the the compiler should first look for an exact match, then matches in the same type class (float != int)!!!, then try upcasting.
Too bad C++ is saddled with those horrible C typing semantics.