dynamic_cast with RefereceCountedObjectPtr


Hello everybody!

I have an inheritance chain of ReferenceCountedObjects and I want to be able to detect the exact type of a specific ReferenceCountedObjectPtr instance.
(each ReferenceCountedObject object has its own ReferenceCountedObjectPtr object)

The compiler rejected all of my dynamic_cast attempts, how can I find the exact type?



ReferenceCountedObjectPtr is not a base, it is a template, so you cannot dynamic_cast such a pointer object.

You could, however, use dynamic_cast on the actual pointer it contains. This would be to the Object, rather than the Ptr class; this is obviously not quite the same as what you’re asking for, but I can’t think of a single reason why casting to the correct Ptr class would be useful, since the object is what matters.



dynamic_cast< MyReferenceCountedClass* > (myRefereceCountedObjectPtr.getObject())

did the job.

Thanks haydxn!