Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   return type of Casting operators (http://www.go4expert.com/forums/return-type-casting-operators-t28229/)

pushpat 19Apr2012 11:49

return type of Casting operators
:drum: hi folks :)

Please explain me why C++ done have return type for casting operator? I have googled, understood that its special case and some points, still I am not clear about why compiler dont give error since casting operators not follow general function syntax and does casting operator different from operator overload?

Thanks in advance

using namespace std;

class paise
        float p ;
        paise(int x =0):p(x){}
        operator int()
                return p;

void print(int x)

int main()
        paise a(4.78);
        return 0;

xpi0t0s 20Apr2012 11:30

Re: return type of Casting operators
I don't understand your question. What output did you get from the above code, and what output did you expect? I would expect:

because 4.78 is passed into a function that takes an int, so the function receives the value 4, which is then assigned to the float p, which is displayed in print().

pushpat 23Apr2012 17:10

Re: return type of Casting operators
I am extremely sorry , i used 'done' instead of "dont" ...

I want to know why overloading the typecast operator doesnot have 'return type' .
i.e operator int(){} doesnt need return type , usually function prototype is
'return-type function_name(arg);

xpi0t0s 23Apr2012 21:16

Re: return type of Casting operators
operator int() casts to int, the return type is already known so it doesn't need specifying again. The syntax if it was specified would be int operator int(), and anything else such as float operator int() would be invalid.

pushpat 27Apr2012 12:23

Re: return type of Casting operators
Thank you xpi0t0s :)

All times are GMT +5.5. The time now is 20:33.