0

If I use int, then sometimes I am to do additional checking of arguments:

class Bad_Value{};
class Value_Overflow{};

int get_area(int a, int b){
  // check arguments
  if (a < 0 || b < 0){
    throw Bad_Value();
  }

  int c = a * b; // the work

  // check the result
  if (b != c / a){
    throw Value_Overflow();
  }
  return c;
}

If I will use unsigned int instead of int then at such cases my code will be more compact:

class Value_Overflow{};

unsigned int get_area(unsigned int a, unsigned int b){
  // arguments checking is not necessary now
  unsigned int c = a * b; // the work

  // check the result
  if (b != c / a){
    throw Value_Overflow();
  }
  return c;
}

But sometimes I had read in some old books what the using int is more preferably than the using of unsigned int. It is still actual?

Andrey Bushman
  • 11,712
  • 17
  • 87
  • 182

0 Answers0