Results 1 to 8 of 8

Thread: dynamic_cast and templates

  1. #1
    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 dynamic_cast and templates

    Hello all,

    I'm attempting to use a templated function to simplify my code. Basically, the function will look at plugins that are loaded, dynamic_cast them to see what kind of plugins they are (they all share a basic interface), then stuff them into a list that is appropriate for the type of plugin.

    It appears, at least with gcc v4.1.2, that dynamic_cast does not work with templates. For example, I have a base class plugin, and a pluginInput class derived from plugin (and pluginInput is the actual plugin interface that QPluginLoader returns... I step it back to plugin to store them all together).

    So now, I have a plugin that correctly associates all its function calls (getName(), etc), and definately is of type pluginInput... but dynamic_cast <T *>(myPlugin) always returns 0 (with T being pluginInput). Is there a way around this? I don't want to static_cast because I don't currently have the capability to tell for certain what kind of plugin it is (which is why I use dynamic_cast).
    Life without passion is death in disguise

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: dynamic_cast and templates

    Try using qobject_cast() instead. This must be used with QPluginLoader results.

    Regards
    Last edited by marcel; 26th July 2007 at 19:03.

  3. #3
    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

    I've found with my heirarchy, I can start with qobject_cast (going from QObject to pluginInput)... but afterwards must use dynamic_cast at all times (not entirely sure why, but I get compiler errors when I try to qobject_cast from plugin (which knows nothing of QObject) to pluginInput (which is derived from QObject)).
    Life without passion is death in disguise

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: dynamic_cast and templates

    Can't you derive plugin from QObject also?

  5. #5
    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

    Quote Originally Posted by marcel View Post
    Can't you derive plugin from QObject also?
    If I did that, then the plugin interface would also derive from QObject, which breaks Qt's plugin rules (the interface may not be derived from QObject, but the plugin itself, derived from the interface, must inherit from both the interface and QObject).
    Life without passion is death in disguise

  6. #6
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: dynamic_cast and templates

    Quote Originally Posted by KShots View Post
    If I did that, then the plugin interface would also derive from QObject, which breaks Qt's plugin rules (the interface may not be derived from QObject, but the plugin itself, derived from the interface, must inherit from both the interface and QObject).
    That's not really a RULE. Merely a recommendation... It's simpler and "better" to stick to using interfaces (if you have any notion of Java this more or less means abstract classes... which QObject obviously is not). However, as long as the plugin interface inherits only from Qt classes and doesn't implement anything by itself (no methods, no variables, ...) it will work just as smooth.
    Current Qt projects : QCodeEdit, RotiDeCode

  7. #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

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

    KShots (7th August 2007)

  9. #8
    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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.