On quarta-feira, 22 de maio de 2013 16.52.29, Jonathan Greig wrote:
> Thank you André. The dynamic_cast worked perfectly. I'm from a C background
> and have been doing C-style casts for years without any major problems.
> Apparently this guy has also:)
> http://stackoverflow.com/questions/28002/regular-cast-vs-static-cast-vs-dyna
> mic-cast

A C cast is equivalent to a chain of reinterpret_cast and const_cast. It does
not do static casting or dynamic casting, which means it will not do pointer
adjustments properly.

So, as a rule of thumb: do not use C-style casts in C++ code and always use
the least invasive C++ cast operator that you can use. And another rule of
thumb: dynamic_casts are always checked[*], all the other casts are never
checked.

In your particular case, you're doing something really obscure and rare: a
cross-cast between two unrelated types. Only dynamic_cast can accomplish that.
So a C-style cast will never work. It would be possible to do it with a
static_cast or a C-style class if you cast to your base type that derives from
both QGraphicsItem and BaseObject.

[*] there are two exceptions, which are when you have to use dynamic_cast yet
you know that it will succeed: cast to through virtual inheritance or your
case.
--
Thiago Macieira - thiago.macieira (AT) intel.com
  Software Architect - Intel Open Source Technology Center

Attachment: signature.asc
Description: This is a digitally signed message part.

_______________________________________________
Interest mailing list
Interest@qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest

Reply via email to