Bad cast not caught after dynamic cast

How is it possible that I successfully pass a try with a pointer not const Triangle?

const Triangle *prim;
const GeometricPrimitive *gp;
try {
    gp = dynamic_cast<const GeometricPrimitive*>(primitives[edges[axis][j].primNum].GetPtr());
    prim = dynamic_cast<const Triangle*>((gp->shape).GetPtr());
}
catch (std::bad_cast& bc) {
    continue;
}

enter image description here

template <typename T> class Reference {
public:
    ...
    T *operator->() { return ptr; }
    const T *operator->() const { return ptr; }
    operator bool() const { return ptr != NULL; }
    const T *GetPtr() const { return ptr; }
private:
    T *ptr;
};

Edit: enter image description here

+4
source share
2 answers

The semantics are dynamic_castdescribed in clause 5.2.7 / 8:

If Cis the type of the class that is referenced or referenced T, the execution time is logically performed as follows:

- If, in the derived object itself (indicated) v, vpoints to (refers) to the public base of the class of the subobject of the object C, and if only one object of the type Cderived from the subobject to which it points (refers) to the vresult (refers) to this object C.

- , v () C, , () C .

- .

9:

- null . std::bad_cast (18.7.2).

. , <typeinfo> std::badcast.

+5

bad_cast : - :

Triangle prim;
GeometricPrimitive gp;
try {
    gp = dynamic_cast<const GeometricPrimitive &>(*(primitives[edges[axis][j].primNum].GetPtr()));

    prim = dynamic_cast<const Triangle &>(*((gp->shape).GetPtr()));

}
catch (std::bad_cast& bc) {
    continue;
}
+4

All Articles