Re: dynamic_cast & Exceptions
Al wrote:
class A {};
class B : public A {};
A* foo();
int main() {
dynamic_cast<B&>(*foo());
return 0;
}
Modulo any typos, this should work,
No: A does not have any virtual function, therefore it doesn't have any RTTI
and you can't dynamic_cast it. That's not your question though, and I'll
assume that it does e.g. have a virtual dtor or some other virtual
function.
assuming foo() returns what is really a B*. Next, if foo() returns
a different derived class of A*, dynamic_cast is supposed to throw
(since it's a reference).
I don't agree with your idea of "works". The code is legal C++ (though maybe
not good C++) and even if it doesn't return a B it will work, just that
that means it will throw a std::bad_cast.
However, what happens if:
i) foo() calls assert();
ii) foo() throws an exception.
In both cases, foo() doesn't return and the dynamic_cast is not evaluated.
Does the dynamic_cast exception override the user exception/assert?
The dynamic_cast is not evaluated, so it doesn't come into effect.
Is it UB, or implementation-specific?
Neither, both are well-defined. BTW: note that throwing an exception that is
not caught will terminate the program, but even if it was caught it
wouldn't change anything here.
Uli
--
Sator Laser GmbH
Gesch??ftsf??hrer: Ronald Boers Steuernummer: 02/858/00757
Amtsgericht Hamburg HR B62 932 USt-Id.Nr.: DE183047360
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]