Hello,
I have the following simple code
//BEGIN CODE
#include <iostream>
class foo { public: virtual void fun( ) { }};
class bar : public foo {public:void fun( ) { }};
int main( )
{
foo f; bar b;
foo* fp1 = &b; foo* fp2 = &f;
bar* bp1 = dynamic_cast<bar*> (fp1);
bar* bp2 = dynamic_cast<bar*> (fp2);
cout << "bp1 = " << bp1 << " bp2 = " << bp2 << endl;
foo& fr1 = b; foo& fr2 = f;
bar& br1 = dynamic_cast<bar&> (fr1);
cout << "Before Incorrect Reference Cast" << endl;
bar& br2 = dynamic_cast<bar&> (fr2);
cout << "br1 = " << &br1 << " br2 = " << &br2 << endl;
}
//END CODE
which produces the following output:
//BEGIN OUTPUT
bp1 = 0xbffff830 bp2 = (nil)
Before Incorrect Reference Cast
Aborted
//END OUTPUT
So, my program is dumping core when I try an invalid dynamic_cast on a
reference. The text I'm using describes the situation for pointers -
that if the cast can't be performed, the null pointer is returned, but
it doesn't address the issue of references at all. So, I have a few
questions:
1) is it valid to dynamic_cast referneces?
2) If so, how can should the program behave when one tries to make a
runtime reference cast which is invalid?
Thanks in advance for any replies!
-exits