A
andreas.koestler
Why does catch (A ex) rather than catch (B &ex) catch the B()
exception object? If I change the catch statement to catch (A &ex) it
doesn't catch B() anymore but catch (B &ex) does.
This might be quite a beginner question but you know, there's no
stupid questions just ....
Thanks Andreas
class A {
public:
A () {
std::cout << "A::A()" << std::endl;
}
virtual void Foo () {
std::cout << "A::Foo()" << std::endl;
}
};
class B : public A {
public:
B () {
std::cout << "B::B()" << std::endl;
}
virtual void Foo () {
std::cout << "B::Foo()" << std::endl;
}
};
int main(int argc, char** argv) {
try {
throw B();
}
catch ( A ex ) { //catch ( A &ex )
ex.Foo ();
}
catch ( B& ex ) {
ex.Foo ();
}
exception object? If I change the catch statement to catch (A &ex) it
doesn't catch B() anymore but catch (B &ex) does.
This might be quite a beginner question but you know, there's no
stupid questions just ....
Thanks Andreas
class A {
public:
A () {
std::cout << "A::A()" << std::endl;
}
virtual void Foo () {
std::cout << "A::Foo()" << std::endl;
}
};
class B : public A {
public:
B () {
std::cout << "B::B()" << std::endl;
}
virtual void Foo () {
std::cout << "B::Foo()" << std::endl;
}
};
int main(int argc, char** argv) {
try {
throw B();
}
catch ( A ex ) { //catch ( A &ex )
ex.Foo ();
}
catch ( B& ex ) {
ex.Foo ();
}