Topic: using bad_cast class
Author: whbloodworth@usa.net (William H. Bloodworth)
Date: 1999/08/24 Raw View
On 23 Aug 99 09:31:40 GMT, "Leonid Verny" <leonidv@tidex.co.il> wrote:
>Hi all.
>I have wrote the wrong code by the Visual C++ ver. 6.00. It is obvious that the function Get of the class AA returns that will be deleted immediately.
>
>If anyone have idea how to catch such bugs using the standard C++ exception classes (bad_alloc etc)
>write me please.
>
... code ommited for brevity...see previous post.
>
>int main(int argc, char* argv[])
>{
> B b;
>
> try
> {
> AA& rAA = dynamic_cast<AA&>(b.Get());
> }
> catch(const std::bad_cast& bad)
> {
> const char* pWhat = bad.what();
> }
>
> return 0;
>};
>Thanks in advance.
>
>Leonid Verny
Leonid,
The dynamic_cast is not technically what is failing. You're causing an
access violation before the dynamic_cast ever has a chance to "do its
business". Rerun your code with these modifications to main() and you'll
see the error "Access violation - no RTTI data!".
int main (void)
{
B b;
try
{
AA & rAA = dynamic_cast<AA&>(b.Get());
}
catch (const exception & error)
{
cerr << error.what() << endl;
}
return 0;
}
William Bloodworth
=====================================================================
= Great Achievement REQUIRES Great Effort.
=
= William H. Bloodworth - whbloodworth@usa."net" ICQ: 21901934
=====================================================================
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Leonid Verny" <leonidv@tidex.co.il>
Date: 1999/08/23 Raw View
This is a multi-part message in MIME format.
------=_NextPart_000_001C_01BEECB4.3F78C3A0
Content-Type: text/plain;
charset="iso-8859-8"
Content-Transfer-Encoding: quoted-printable
Hi all.
I have wrote the wrong code by the Visual C++ ver. 6.00. It is obvious =
that the function Get of the class AA returns that will be deleted =
immediately.
If anyone have idea how to catch such bugs using the standard C++ =
exception classes (bad_alloc etc)
write me please.
#include <typeinfo>
class A
{
public:
A(){;};
virtual ~A(){};
};
class AA:public A
{
public:
AA(){;};
~AA(){};
};
class B
{
public:
A& Get();
};
A& B::Get()
{
AA rAA();
return rAA;
};
using namespace std;
int main(int argc, char* argv[])
{
B b;
=20
try
{
AA& rAA =3D dynamic_cast<AA&>(b.Get());
}
catch(const std::bad_cast& bad)
{
const char* pWhat =3D bad.what();
}
=20
return 0;
};
Thanks in advance.
Leonid Verny
------=_NextPart_000_001C_01BEECB4.3F78C3A0
Content-Type: text/html;
charset="iso-8859-8"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD W3 HTML//EN">
<HTML>
<HEAD>
<META content=3D"text/html; charset=3Dwindows-1255" =
http-equiv=3DContent-Type>
<META content=3D'"MSHTML 4.72.3110.37"' name=3DGENERATOR>
</HEAD>
<BODY>
<DIV>Hi all.</DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2>I have wrote the wrong =
code by the=20
Visual C++ ver. 6.00. It is obvious that the function Get of the class =
AA=20
returns that will be deleted immediately.</FONT></DIV>
<DIV> </DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2>If anyone have idea how =
to catch such=20
bugs using the standard C++ exception classes (bad_alloc =
etc)</FONT></DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2></FONT><FONT =
face=3DArial size=3D2>write=20
me please.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><STRONG><FONT size=3D2>#include =
<typeinfo></STRONG></FONT></DIV>
<DIV><STRONG><FONT size=3D2></STRONG></FONT> </DIV>
<DIV><STRONG><FONT size=3D2>class =
A<BR>{<BR>public:<BR> =20
A(){;};<BR> virtual ~A(){};</STRONG></FONT></DIV>
<DIV><STRONG><FONT size=3D2>};</STRONG></FONT></DIV>
<DIV><STRONG><FONT size=3D2></STRONG></FONT> </DIV>
<DIV><STRONG><FONT size=3D2>class AA:public=20
A<BR>{<BR>public:<BR> AA(){;};<BR> =20
~AA(){};<BR>};</STRONG></FONT></DIV>
<DIV><STRONG><FONT size=3D2></STRONG></FONT> </DIV>
<DIV><STRONG><FONT size=3D2>class =
B<BR>{<BR>public:<BR> A&=20
Get();<BR>};</STRONG></FONT></DIV>
<DIV><STRONG><FONT size=3D2></STRONG></FONT> </DIV>
<DIV><STRONG><FONT size=3D2>A& B::Get()<BR>{<BR> =
AA=20
rAA();<BR> return rAA;<BR>};</STRONG></FONT></DIV>
<DIV><STRONG><FONT size=3D2></STRONG></FONT> </DIV>
<DIV><STRONG><FONT size=3D2>using namespace std;</STRONG></FONT></DIV>
<DIV><STRONG><FONT size=3D2></STRONG></FONT> </DIV>
<DIV><STRONG><FONT size=3D2>int main(int argc, char*=20
argv[])<BR>{<BR> B b;<BR> =
<BR> =20
try<BR> =
{<BR> =20
AA& rAA =3D =
dynamic_cast<AA&>(b.Get());<BR> =20
}<BR> catch(const std::bad_cast&=20
bad)<BR> =
{<BR> const=20
char* pWhat =3D bad.what();<BR> =
}<BR> =20
<BR></FONT></STRONG><STRONG><FONT size=3D2> return=20
0;<BR>};</STRONG></FONT></DIV>
<DIV><STRONG><FONT size=3D2></STRONG></FONT> </DIV>
<DIV> </DIV>
<DIV><FONT color=3D#000000 face=3DArial><FONT size=3D3>Thanks in=20
advance.</FONT></FONT><FONT size=3D3></FONT></DIV>
<DIV><FONT color=3D#000000 face=3DArial><FONT =
size=3D3></FONT></FONT><FONT=20
size=3D3></FONT> </DIV>
<DIV><FONT color=3D#000000 face=3DArial><FONT size=3D3>Leonid=20
Verny</FONT></FONT></DIV>
<DIV><FONT color=3D#000000 face=3DArial><FONT =
size=3D3></FONT></FONT> </DIV>
<DIV><FONT color=3D#000000 face=3DArial><FONT =
size=3D3></FONT></FONT><FONT=20
size=3D3></FONT> </DIV>
<DIV><FONT color=3D#000000 face=3DArial =
size=3D2></FONT> </DIV></BODY></HTML>
------=_NextPart_000_001C_01BEECB4.3F78C3A0--
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]