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).
Re: dynamic_cast and templates
Try using qobject_cast() instead. This must be used with QPluginLoader results.
Regards
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)).
Re: dynamic_cast and templates
Can't you derive plugin from QObject also?
Re: dynamic_cast and templates
Quote:
Originally Posted by
marcel
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).
Re: dynamic_cast and templates
Quote:
Originally Posted by
KShots
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.
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
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...