Consider the following piece of code
catch (Base& w)
{
...
throw;
}
catch (Base& w)
{
...
throw w;
}
What do you think is the difference between the 2 approaches above .
In the first case , the exception is re thrown . In the second case a copy is made and a new exception is thrown . Also the copy is based on the static type i.e
Base (copy constructor is called )
Wednesday, April 02, 2008
C++ : Object Copies in C++ are based on Object's Static Type Not Dynamic Type
Consider the following piece of code
class Base { ... };
class Derived: public Base { ... };
void passAndThrowDerived()
{
Derived local;
...
Base& rw = local;
throw rw;
}
In the above mentioned case . When a copy of rw is made while throwing it .
The copy constructor for the type is called . In this case the since rw is a reference to Base there fore the copy constructor of Base is called rather than the
Copy Constructor of Derived class.
class Base { ... };
class Derived: public Base { ... };
void passAndThrowDerived()
{
Derived local;
...
Base& rw = local;
throw rw;
}
In the above mentioned case . When a copy of rw is made while throwing it .
The copy constructor for the type is called . In this case the since rw is a reference to Base there fore the copy constructor of Base is called rather than the
Copy Constructor of Derived class.
C++ : Why are objects thrown as exception always copied
When an exception is thrown , the objects are generally copied. To explain this consider the following :
{
MyObject myObject;
cin >> myObject;
throw myObject;
}
if the object myObject was passed by reference then the same myObject would be thrown out . If the same object is thrown out then as soon as it goes out of scope , its destructor would get called and as a result of which some garbage would finally reach the exceptional handling code .
It is for this reason that copies of myObject are thrown rather than the original one .
{
MyObject myObject;
cin >> myObject;
throw myObject;
}
if the object myObject was passed by reference then the same myObject would be thrown out . If the same object is thrown out then as soon as it goes out of scope , its destructor would get called and as a result of which some garbage would finally reach the exceptional handling code .
It is for this reason that copies of myObject are thrown rather than the original one .
Subscribe to:
Posts (Atom)
Labels
. linux
(1)
algorithm
(15)
analytics
(1)
bash
(2)
bigoh
(1)
bruteforce
(1)
c#
(1)
c++
(40)
collections
(1)
commands
(2)
const
(1)
cosine similarity
(1)
creating projects
(1)
daemon
(1)
device_drivers
(1)
eclipse
(6)
eclipse-plugin-development
(9)
equals
(1)
formatting
(1)
freebsd
(1)
game programming
(1)
hashcode
(1)
heap
(1)
heaps
(1)
immutable-objects
(1)
java
(19)
JDT
(1)
kernel
(1)
linux
(4)
little sugar
(23)
logging
(1)
machine learning
(1)
marker-resolution
(1)
markers
(1)
mergesort
(1)
mixins
(1)
numbers
(1)
opengl
(2)
patterns
(2)
priority-queue
(1)
programming
(51)
ps
(1)
ranking
(1)
refactoring
(3)
references
(1)
security
(1)
set
(1)
shell
(1)
similarity
(1)
statistics
(1)
stl
(1)
tetris
(1)
threads
(1)
trees
(2)
unicode
(1)
unix
(2)
views
(2)
windows programming
(2)
XNA
(1)