Search code examples
c++qtqt5qshareddata

QT qSharedDataPointer dynamic casting?


I am trying to perform the following cast and I can't find a way either in the QT documentation or online to make this dynamic cast work which has been confusing:

class Entity : public QSharedData
{
  public:
    typedef QExplicitlySharedDataPointer<Entity> Pointer;
    typedef QExplicitlySharedDataPointer<const Entity> ConstPointer;
...
}

class EntityExtended : public Entity
{
public:
    typedef QExplicitlySharedDataPointer<EntityExtended> Pointer;
    typedef QExplicitlySharedDataPointer<const EntityExtended> ConstPointer;
...
}



bool SomeClass::createEntity(const Entity::ConstPointer entity)
{
  auto extendedEntity = dynamic_cast<const EntityExtended::ConstPointer>(entity);
}

The above produces the error:

error C2680: 'const EntityExtended::ConstPointer': invalid target type for dynamic_cast
note: target type must be a pointer or reference to a defined class

What am I missing?


Solution

  • The problem is that you can only use dynamic_cast on raw pointers. Even though QExplicitlySharedDataPointer is a class that's meant to be used like a pointer, it's not a raw pointer, so you can't use dynamic_cast on it.

    The solution is to get the raw pointer using QExplicitlySharedDataPointer's data() or constData() methods, and use dynamic_cast on the raw pointer instead. You can then convert it back to a QExplicitlySharedDataPointer by passing the raw pointer to the QExplicitlySharedDataPointer constructor:

    bool SomeClass::createEntity(const Entity::ConstPointer entity)
    {
      //Create a raw pointer to use with dynamic_cast
      const EntityExtended *rawPointer = dynamic_cast<const EntityExtended*>(entity.constData());
    
      //Convert the raw pointer back to a QExplicitlySharedDataPointer
      EntityExtended::ConstPointer extendedEntity = EntityExtended::ConstPointer(rawPointer);
    
      if(extendedEntity == nullptr){
          //*entity is not an instance of ExtendedEntity
      }
      else{
          //*entity is an instance of ExtendedEntity
      }
    }