Hi All
I am reviewing C++ casts operator and I have the following doubt:
for polymorphic classes
- I I should use
polymorphic_cast
- I should never use of
static_cast
since down-casting might carry to undefined behavior. The code compiles this case anyway.
Now suppose that I have the following situtation
class CBase{};
class CDerived : public CBase{};
int main( int argc, char** argv ){
CBase* p = new CDerived();
//.. do something
CDerived*pd = static_cast<CDerived*>( p );
}
Since there is no polymorphism involved I will not use polymorphic_cast
and the code will not even compile.
If at some point, someone introduces some virtual
functions in the inheritance tree and I am now aware of it so I am in danger: how can I realize it?
I should move to polymorphic_cast
to avoid any risk, but the code will be still compiling without any notification.
What do you do to realize about such kind of changes or prevent these case?
Thanks AFG