Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

dynamic_cast problems

270 views
Skip to first unread message

A Ratcliffe

unread,
Jul 9, 2002, 4:22:34 PM7/9/02
to
As mentioned in a previous post, I generaly only use MFC for prototyping these days, but I've run up against something in VC++.NET which doesn't seem to make sense, in the MFC environment.
 
I have a CDocument-derived class (CPlasmaDoc). This is wizard-generated, and set as DYNCREATE. In a menu-message handler for CMainFrame, I call GetActiveDocument, which returns a CDocument*. Therefore, I use:-
 
CPlasmaDoc* doc;
 
doc = dynamic_cast<CPlasmaDoc*>(GetDocument());
 
But when I do so, I get the following errors:-
 
First-chance exception at 0x10226927 (msvcr70d.dll) in Plasma.exe: 0xC0000005: Access violation reading location 0x00000004.

First-chance exception at 0x77e989d1 in Plasma.exe: Microsoft C++ exception: __non_rtti_object @ 0x0012f924.

Unhandled exception at 0x77e989d1 in Plasma.exe: Microsoft C++ exception: __non_rtti_object @ 0x0012f924.

Now since RTTI is provided by IMPLEMENT_DYNAMIC, and the documentation clearly states that IMPLEMENT_DYNCREATE has all DYNAMIC's functionality, why is it complaining about a non RTTI object? Since I know in this instance it can be downcast, I could just do it anyway, but I consider casts an area to take care with, especially since the number of document types could be increased, in which case this cast may not be true (hence the next step after casting is to see if 'doc == NULL').

Yours,

 

A Ratclife

aratc...@archimagic.net

 

nobody

unread,
Jul 9, 2002, 4:41:05 PM7/9/02
to
Try DYNAMIC_DOWNCAST instead of dynamic_cast.

Ken Alverson

unread,
Jul 9, 2002, 4:44:03 PM7/9/02
to
MFC has it's own RTTI which is unrelated to the RTTI in the C++ standard.  To use MFC's RTTI, you need to use IsKindOf followed by a static cast.
 
You're trying to use C++'s RTTI, which is well and good, but it is disabled by default.  You need to go into your project settings and enable C++ RTTI to allow the dynamic_cast to work.  C++'s RTTI will work regardless the IMPLEMENT_DYNAMIC macro.
 
Ken

A Ratcliffe

unread,
Jul 14, 2002, 7:42:57 AM7/14/02
to
Thanks for the assistance. I should have figured that MFC had its own perculiar way of doing things, instead of using the built in C++ RTTI. After all, look what it did to EXCEPTIONs for a long time....ughhh.
 
BTW, once I've activated the C++ RTTI, can I use C++ RTTI for all my type identification in MFC, or will MFC complain if I don't use its own brand of RTTI in some areas of code?
 
Yours,
 
A Ratcliffe

Jason Barlow

unread,
Jul 14, 2002, 5:58:32 PM7/14/02
to
I would not rely on it. MFC's DYNAMIC_DOWNCAST et. al. goes all the way
back to its origins, when most compilers had no support for RTTI (RTTI was a
pretty new concept - it may not even have been part of the standard back
then). The C++ standard defines a number of constraints about which types
can be successfully downcast using dynamic_cast. Additionally it defines
certain behaviors for when dynamic_downcast failes. While the behavior of
DYNAMIC_DOWNCAST is similar, I don't believe it provides the exact same
functionality as a dynamic_downcast (for instance, cross-casting or casting
of a reference type) nor does it operate under the same restrictions.

While the two may work interchangeably for 99% of the cases, I would be
loathe to use it except on my own data types in fear of being bit by that
finge condition and losing a week of sleep to a silly bug. But then that's
just me :)

J

A Ratcliffe" <aratc...@archimagic.co.uk> wrote in message

news:#eJastyKCHA.2052@tkmsftngp10...

Ken Alverson

unread,
Jul 17, 2002, 4:02:26 PM7/17/02
to
As Jason pointed out, MFC's RTTI (and also MFC's exceptions) pre-date the C++ versions of the same, that's why they exist in the first place.  Backwards source compatibility mostly explains their continued use.
 
C++ RTTI *should* do everything you want, including multiple inheritance, which MFC's RTTI will not do.  *HOWEVER*, eliminating the MFC RTTI is likely to brake parts of the MFC framework, like perhaps serialization.  Therefore, while it should be OK for you to switch from DYNAMIC_DOWNCAST to dynamic_cast<>, you don't want to get rid of your MFC RTTI declarations, because the framework will still be using the MFC casts.
 
Ken
0 new messages