13-02-2017, 04:31 PM
Dear MISRA team,
the term "unrelated type" leads to the question whether casting from a base to derived class pointer type and vice versa using reinterpret_cast shall be covered by the rule or not. Remark: I am perfectly aware that dynamic_cast and static_cast , resp., would be the right way for these purposes, so my question is rather academic.
I refer to the term "related type" as Stroustrup uses it concerning types "in the same class hierarchy" which is surely the case here.
the term "unrelated type" leads to the question whether casting from a base to derived class pointer type and vice versa using reinterpret_cast shall be covered by the rule or not. Remark: I am perfectly aware that dynamic_cast and static_cast , resp., would be the right way for these purposes, so my question is rather academic.
I refer to the term "related type" as Stroustrup uses it concerning types "in the same class hierarchy" which is surely the case here.
Code:
class B {};
class D : public B {};
void f()
{
D* dptr = new D();
B* bptr = reinterpret_cast( dptr );
D* dptr2 = reinterpret_cast( bptr ); // correct (yet ugly) when one knows where the base class pointer points to
}
<t></t>