Type Casting in cin ( C++ ) -
int x; cout << "enter integer :: " << endl; cin >> x ; cout << "your value = " << x << endl; cout << "enter float :: " << endl; cin >> float (x) ; cout << "your value = " << x << endl;
the above code shows error. why can type cast in cout not in cin ?
a cast this:
float(x)
produces nameless temporary object of type float
. >> operator looks this:
istream & operator>>( istream &, float & f );
and can't bind non-const reference temporary.
the output operator looks this:
ostream & operator<<( ostream &, const float & f );
and can bind const reference temporary, works.
Comments
Post a Comment