How would I overload the = operator in a way that I could execute
int someInt;
MyClass instanceOfMyClass;
someInt = instanceOfMyClass;
Where MyClass contains an integer named number?
How would I overload the = operator in a way that I could execute
int someInt;
MyClass instanceOfMyClass;
someInt = instanceOfMyClass;
Where MyClass contains an integer named number?
I would avoid doing this in most cases as it can be difficult to tell what exactly is happening. That said:
class MyClass {
public:
operator int() { return number; }
private:
int number;
};
This creates an (implicit) conversion operator for your class.
NextRandomNumber() or something? It's possible doing this will obscure what the call does for no real benefit other than feeling clever.