[Interest] qobject_cast

Till Oliver Knoll till.oliver.knoll at gmail.com
Thu Apr 20 18:52:37 CEST 2017



> Am 20.04.2017 um 08:05 schrieb nikita baryshnikov <nib952051 at gmail.com>:
> 
> Hi, Igor
> 
> Your example is incorrect, cause you forget about Q_OBJECT macro in
> Channel class

That's what I just wanted to add as a general remark, because it is a common pitfall (at least for me): /every/ class which - directly or indirectly - inherits from QObject must have a Q_OBJECT declaration, /even/ if it doesn't emit or consume Qt signals or provides slots. IF that class is expected to behave nicely with said qobject_cast "and friends" (which is almost alway a desireable feature).

Q_OBJECT not only provides the glue to provide "slot" methods, but also generates the required meta information.

In fact, IIRC that's another advantage of qobject_cast over dynamic_cast: it works by design "across library boundaries" (dynamic_cast might or might not work, depending on whether the libraries share the same address space or other compiler-specific settings). 

Cheers,
  Oliver


More information about the Interest mailing list