473,387 Members | 1,495 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

exception handling and reference

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
Jul 22 '05 #1
3 1413
"nikola" <se******@here.lol> wrote in message news:PxIsc.240389
class Matherr{
class int_overflow : public Matherr{ catch(Matherr &m){ 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()?


For the same reason that in

f(Matherr m);
int main() { int_overflow io; f(io); }

the system slices off the int_overflow part of io, and makes a copy of the
Matherr part only, and calls f. In this function a call to
m.virtualfunction() will call Matherr::virtualfunction().

But had you done

f(Matherr& m);

then you're passing the original io object, and a call to
m.virtualfunction() calls int_overflow::virtualfunction().

So passing and catching by reference you preserve the original type and
avoid making a copy, and passing and catching by value you slice off the
derived type and make an extra copy.
Jul 22 '05 #2
On Tue, 25 May 2004 14:15:11 GMT, "nikola" <se******@here.lol> wrote:
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()?


When you copy a derived object into a base object, the object is
"sliced". This is why you have to use pointers or references to
achieve polymorphic behaviour. Have a read of this FAQ:

http://www.parashift.com/c++-faq-lit...semantics.html

Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #3
thanx both :)
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Maurice LING | last post by:
Hi, I'm trapping exceptions with these codes... try: except IOError: except TypeError: except:
7
by: Ekim | last post by:
hy, I've a question concerning exception-handling in c++: is there a possibility to catch any exception (I know one can do that by "catch(...)") and display its default-error-message (like in...
11
by: Master of C++ | last post by:
Hi, I am writing a simulation package in C++, and so far I've written about 8000 lines of code and have about 30 classes. I haven't used C++ exceptions so far (for various reasons). The only two...
6
by: Josh Mcfarlane | last post by:
I keep trying to get myself out of the return-code mindset, but it doesn't seem to work. They are suppose to get rid of if-then statements of return codes, but you still have to do an if statement...
3
by: mailar | last post by:
Hi, Can someone tell me how to perform exception handling in DB2 UDFs(not procedures). ALso, while usin the SIGNAL statement in one of my UDFs I encountered an error message which is as below ...
5
by: juergen perlinger | last post by:
Hello out there. sometimes I need to have proper control of the floating point arithmetic of the C(and C++) runtime system, and using the f.p. exception handling of the C99 standard is quite...
2
by: tom | last post by:
Hi, I am developing a WinForm application and I am looking for a guide on where to place Exception Handling. My application is designed into three tiers UI, Business Objects, and Data Access...
44
by: craig | last post by:
I am wondering if there are some best practices for determining a strategy for using try/catch blocks within an application. My current thoughts are: 1. The code the initiates any high-level...
3
by: Gary | last post by:
I'm in the application_error event. I want to know which aspx experienced the exception. Is there any easy way? Thanks, G
1
by: stephen | last post by:
Hi, I am a lil confused about exception handling. I have a main app and i am importing 2 DLL's. one of the DLL's is for connection/datasets etc and the other has logging errors to file.Both...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.