Trouble with dynamic_cast and references

E

exits funnel

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
 
T

Thomas Tutone

exits funnel said:
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.

What text are you using?
So, I have a few
questions:
1) is it valid to dynamic_cast referneces?
Yes.

2) If so, how can should the program behave when one tries to make a
runtime reference cast which is invalid?

It throws a std::bad_cast exception. Since your program does not have try
and catch bodies, the exception is uncaught - so the program aborts.

By the way, you need to use std::cout and std::endl rather than just cout
and endl (or throw in some using declarations).

Best regards,

Tom
 
E

exits funnel

What text are you using?

'Thinking in C++' by Bruce Eckel
It throws a std::bad_cast exception. Since your program does not have try
and catch bodies, the exception is uncaught - so the program aborts.

Thanks, that explains (and fixes) it. Come to think of it, Eckel hasn't
covered exception handling yet, which is probably why he glossed over
the issue as he's usually pretty thorough.
By the way, you need to use std::cout and std::endl rather than just cout
and endl (or throw in some using declarations).

Good point, thanks again.

-exits
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top