wxDynamicCast doesn't work

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
cneng3
Earned a small fee
Earned a small fee
Posts: 13
Joined: Sat Apr 28, 2018 7:43 pm

wxDynamicCast doesn't work

Postby cneng3 » Sat May 05, 2018 8:32 pm

Hey all,

I have a piece of fully functional code that uses std::dynamic_cast, however, I recently read about using RTTI is a bad practice (https://www.wxwidgets.org/develop/codin ... /#m_prefix), so I decided to switch to wxDynamicCast. The problem here is that after I change dynamic_cast to wxDynamicCast in my code, the program crashes immediately.

User avatar
doublemax
Moderator
Moderator
Posts: 12320
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxDynamicCast doesn't work

Postby doublemax » Sat May 05, 2018 8:38 pm

These coding guidelines are for code inside of wxWidgets itself.

Please show the code, the old one that works and the new one that crashes.
Use the source, Luke!

cneng3
Earned a small fee
Earned a small fee
Posts: 13
Joined: Sat Apr 28, 2018 7:43 pm

Re: wxDynamicCast doesn't work

Postby cneng3 » Sat May 05, 2018 8:50 pm

so I changed

Code: Select all

Worker::Worker(wxWindow* parent)
{
   m_text = dynamic_cast<MyFrame*>(parent)->StaticText1;
}

to

Code: Select all

   m_text = wxDynamicCast(parent, MyFrame)->StaticText1;

User avatar
doublemax
Moderator
Moderator
Posts: 12320
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxDynamicCast doesn't work

Postby doublemax » Sat May 05, 2018 9:09 pm

Is "parent" guaranteed to be of type "MyFrame"? wxDynamicCast(parent, MyFrame) will return NULL, if it's not and then of course the code will crash.

For safety you should always check the return value of wxDynamicCast.

Code: Select all

Worker::Worker(wxWindow* parent)
{
  MyFrame *f = wxDynamicCast( parent, MyFrame );
  if( f != NULL )
  {
     // more code here
  }
}
Use the source, Luke!

cneng3
Earned a small fee
Earned a small fee
Posts: 13
Joined: Sat Apr 28, 2018 7:43 pm

Re: wxDynamicCast doesn't work

Postby cneng3 » Sat May 05, 2018 9:17 pm

yes. "parent" is definitely a type of "MyFrame". But why would wxDynamicCast ever return NULL since this piece of code works properly with dynamic_cast?

New Pagodi
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 248
Joined: Tue Jun 20, 2006 6:47 pm
Contact:

Re: wxDynamicCast doesn't work

Postby New Pagodi » Sat May 05, 2018 9:22 pm

I think wxDynamicCast uses the wxWidgets RTTI system. So in order to be able to cast to that class type you have to use the wxDECLARE_DYNAMIC_CLASS/wxIMPLEMENT_DYNAMIC_CLASS or wxDECLARE_ABSTRACT_CLASS/wxIMPLEMENT_ABSTRACT_CLASS macros to make that class type known to the RTTI system.

As the docs say:

The standard C++ RTTI can be used in the user code without any problems and in general you shouldn't need to use the functions and the macros in this section unless you are thinking of modifying or adding any wxWidgets classes


So unless you need some features only supported by the wxWidgets RTTI system, you should probably use the standard C++ one instead.


Return to “C++ Development”

Who is online

Users browsing this forum: Baidu [Spider], Bing [Bot], Google [Bot] and 8 guests