1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

return type of Casting operators

Discussion in 'C++' started by pushpat, Apr 19, 2012.

  1. pushpat

    pushpat New Member

    :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;
  2. xpi0t0s

    xpi0t0s Mentor

    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().
  3. pushpat

    pushpat New Member

    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);
  4. xpi0t0s

    xpi0t0s Mentor

    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.
  5. pushpat

    pushpat New Member

    Thank you xpi0t0s :)

Share This Page