[Interest] Container members in abstract base class?

Thiago Macieira thiago.macieira at intel.com
Wed May 22 21:34:26 CEST 2013


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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 190 bytes
Desc: This is a digitally signed message part.
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20130522/b4120cff/attachment.sig>


More information about the Interest mailing list