westhood wrote:
In my program I must have some variables which values are infinity.
It means the variable must be
bigger than any integer. And if we add some to it, its value should
still be infinity.
I try to use the max value of int ,but it will cause overflow if you
add something to it.
I also try to write a class INF,in which I overload operator + and >.
But those variables sometimes
should be integer,i can not make them be the in instances of INF.
Please help me to the resolve problem !
If you design your INF class, you could still try to provide the
'operator int() const' (so called "conversion function"), but I am
not sure what you're going to return from it. INT_MAX, perhaps?
Which doesn't sound like a good idea...
It seems that you need to design your own wrapper around 'int', and
*extend* it by allowing it to have the value "infinity". Do NOT let
it be convertible _to_ int, only _from_ int. You will have to give
it all the traits of 'int' (overload all the operators you need to
use it with, like + - * / % @= < <= >= == != ^ | & ~). Seems like
lots of work, but it's not really. You only need to overload the
ones you use in your program. Also, make sure that to overload the
binary ones (except the compound assignments) as non-members, it'll
allow you to write
MyInt a(42);
2 + a;
This is a very good exercise how to properly *extend* some other type.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask