K
ksharma
#include <iostream>
#include <map>
using namespace std;
class Base {
public:
~Base() {}
virtual void example() {
cout <<"Base Example"<<endl;
}
};
class Derived {
public:
~Derived() {}
void example() {
cout <<"Derived Example"<<endl;
}
};
void main()
{
// Outer try block.
try {
//Inner try block.
try {
Derived d;
throw d; // ||| C...R....A....S....H
}
//Catch by reference -- won't slice.
catch (Base& ex) {
ex.example(); //O.K.
//Rethrow ...
throw ex; //Mistake -- slices!
//Should just use "throw;".
}
//END Inner try block.
}
// Should be fine, but ...
catch(Base& ex) {
ex.example(); //... not what we expected!
}
}
#include <map>
using namespace std;
class Base {
public:
~Base() {}
virtual void example() {
cout <<"Base Example"<<endl;
}
};
class Derived {
public:
~Derived() {}
void example() {
cout <<"Derived Example"<<endl;
}
};
void main()
{
// Outer try block.
try {
//Inner try block.
try {
Derived d;
throw d; // ||| C...R....A....S....H
}
//Catch by reference -- won't slice.
catch (Base& ex) {
ex.example(); //O.K.
//Rethrow ...
throw ex; //Mistake -- slices!
//Should just use "throw;".
}
//END Inner try block.
}
// Should be fine, but ...
catch(Base& ex) {
ex.example(); //... not what we expected!
}
}