N
nikola
Hi all,
If I have these two simple classes to handle matherr
class Matherr{
public:
virtual void print_err() const{
std::cerr << "Error";
}
};
class int_overflow : public Matherr{
private:
const char *op;
int a, b;
public:
int_overflow(const char *p, int a1, int a2){
op = p; a = a1; b = a2;
}
void print_err() const{
std::cerr << op << " (" << a << ", " << b << ")";
}
};
And the main()...
void main(int argc, char **argv){
try{
int i1 = add(1, 2) ;
int i2 = add(INT_MAX, -2);
int i3 = add(INT_MAX, 2); //Exception!
}
catch(Matherr &m){
m.print_err();
}
}
int add(int x, int y){
if((x > 0 && y > 0 && x > INT_MAX - y) || (x < 0 && y < 0 && x <
INT_MIN + y))
throw int_overflow("+", x, y);
return x + y;
}
Why if I catch using the reference (&m) it calls int_overflow's print_err()
and if I use it by value (m) it calls matherr's print_err()?
Thanx
If I have these two simple classes to handle matherr
class Matherr{
public:
virtual void print_err() const{
std::cerr << "Error";
}
};
class int_overflow : public Matherr{
private:
const char *op;
int a, b;
public:
int_overflow(const char *p, int a1, int a2){
op = p; a = a1; b = a2;
}
void print_err() const{
std::cerr << op << " (" << a << ", " << b << ")";
}
};
And the main()...
void main(int argc, char **argv){
try{
int i1 = add(1, 2) ;
int i2 = add(INT_MAX, -2);
int i3 = add(INT_MAX, 2); //Exception!
}
catch(Matherr &m){
m.print_err();
}
}
int add(int x, int y){
if((x > 0 && y > 0 && x > INT_MAX - y) || (x < 0 && y < 0 && x <
INT_MIN + y))
throw int_overflow("+", x, y);
return x + y;
}
Why if I catch using the reference (&m) it calls int_overflow's print_err()
and if I use it by value (m) it calls matherr's print_err()?
Thanx