when a operator function is implemented as a member function left most operand must be object of operator's class but in code below
#include<iostream>
using namespace std;
class ffloat {
private:
float a;
public:
ffloat():a(34.566){}
operator int () {
return a=static_cast<int >(a);
}
};
int main() {
ffloat w;
int x;
x=w;
cout<<x<<endl;
}
left most operand is variable,then why compiler didn't send a error Thanks in advance....
return a=static_cast<int >(a);Do you understand what this does? If you made your operator a const function, the compiler would prevent this probable mistake.ain the object.ffloat w; cout << static_cast<int>(w); cout << "," << w << endl;ought to display34 34.566but in your code it will display34 34.