473,322 Members | 1,431 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Trouble with dynamic_cast and references

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

Jul 22 '05 #1
2 1847
"exits funnel" <ex*********@NOSPAMyahoo.com> wrote:
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
Jul 22 '05 #2
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

Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: alg | last post by:
dynamic_cast<> comes in play when to perform conversion from a pointer to a base class to a pointer to a derived class. I don't understand: 1. why this is so necessary since we can either use...
0
by: Dan Noland | last post by:
- Hello All - I believe that I have a firm understading of dynamic_cast and its proper usage, however g++-2.95.4 is telling me that something I believe to be possible is not possible. I have:...
13
by: GianGuz | last post by:
Everyone knows about the complex and cpu-expensive procedures taken by dynamic_cast to find the right function call in a virtual classes hierarchy. The question I would to rise is when...
8
by: Thomas Lorenz | last post by:
Hello, first, I didn't find any reference to my question through googling. dynamic_cast uses RTTI to determine if the cast expression is valid. Invalid casts of pointers give a '0'-result. As...
3
by: Ganesh | last post by:
On devx site, I saw following code. It says when a derived class is tried to cast to base type, it looks at the missing vtable and complains if the object is already deleted. I am of the opinion...
22
by: Boris | last post by:
I'm porting code from Windows to UNIX and ran into a problem with dynamic_cast. Imagine a class hierarchy with three levels: class Level2 derives from Level1 which derives from Base. If you look...
8
by: pietromas | last post by:
In the example below, why does the dynamic_cast fail (return NULL)? It should be able to cast between sibling classes ... #include <iostream> class A { public: virtual const int get()...
15
by: Bo Yang | last post by:
Hi, I can understand static_cast, reinterpret_cast and const_cast, they all work at compile time. But I can figure out how the C++'s dynamic- cast works? Could you please explain how for me?...
25
by: lovecreatesbea... | last post by:
Suppose I have the following three classes, GrandBase <-- Base <-- Child <-- GrandChild The following cast expression holds true only if pBase points object of type of ``Child'' or...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.