Results 1 to 8 of 8

Thread: dynamic_cast and templates

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: dynamic_cast and templates

    For a discussion on the differences between dynamic_cast and qobject_cast (and also the problems with dynamic_cast), see:
    http://www.qtcentre.org/forum/f-qt-p...dget-5037.html

    The trouble with dynamic_cast and templates boils down to the following issue:
    * dynamic_cast (as implemented by gcc) uses a pointer to some type_info structure for testing types
    * this structure exists multiple times if you instantiate a template in different translations units (ok, maybe only in different libs)
    Therefore, using the 'same' templated type does not work as the dynamic_cast at one place uses a pointer to an equal but not the same type_info structure.

    Fixes:
    - maybe there is a common base type that you can factor out (i.e. templates are used in the subclasses, but you dynamic_cast to the base type)
    - use qobject_cast instead (of course, only applicable with QObjects)
    - use some other rtti mechanism and cast with static_cast

    HTH
    Christoph

  2. The following user says thank you to caduel for this useful post:

    KShots (7th August 2007)

  3. #2
    Join Date
    Feb 2006
    Location
    USA
    Posts
    142
    Thanks
    24
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: dynamic_cast and templates

    Hmm... according to that thread, I need to build my pure virtual base class into a shared library which then my main app and each of my plugins would link against (to avoid multiple instances of an moc object from using the Q_OBJECT macro).

    Currently, everything is using the interface header (the main app and the plugins have a HEADERS += line for it). I'll see if this works in my situation...
    Life without passion is death in disguise

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.