Results 1 to 8 of 8

Thread: dynamic_cast and templates

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #7
    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)

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.