i'm writing a class like this :
class Digit
{
private :
int *ref;
public :
Digit (int a) : ref(&a) {}
int get_val ()
{
return (*ref);
}
Digit operator= (int &a)
{
if (a < 0 || a > 9)
throw "invalid digit assignment.";
(*ref) = a;
return (*this);
}
};
so if someone define a digit ,he can not assign value more than 9 or less than 0 to it. the problem is, i also want to define another = operator, that he can assign a Digit to some value too. i tried something like this :
int operator= (int &a, Digit &d)
{
a = d.get_value();
return a;
}
but i get some error : 'int operator=(int&, Digit&)' must be a nonstatic member function
how can i fix this? how can i overload operator = with 2 arguments?
What you really need may be operator int()
.
class Digit {
operator int() { return get_val(); }
};
Then you can write int a = d;
assuming d
is of class Digit
.