What is the proper way to call () operator overloaded to return a float
class MyClass {
public:
operator float () const {
return m_some_float_value;
}
private:
float m_some_float_value
}
>Solution :
You want this:
class MyClass {
public:
operator float() const {
return m_some_float_value;
};
private:
float m_some_float_value = 1.0f; // initialize with 1.0f for
// demo purposes
};
int main() {
MyClass c;
float x = c; // just do the assignment and
// the float operator will be called
// now x contains 1.0f
}