Out of habit for checking null pointers, I sometimes wrote:
MyClass * c = someBasePtr ? dynamic_cast<MyClass*>(someBasePtr) : 0; if (c) {...
In fact, checking the null pointer before moving on to the dynamic hyphenation, as well as checking the return.
Then I read in the MSDN documentation
A null pointer value is converted to a null pointer destination type dynamic_cast.
It appears that I could safely remove the construct?:. Is this porting C ++?
So the new code will be
MyClass * c = dynamic_cast<MyClass*>(someBasePtr); if (c) {...
Of course, assuming that someBasePtr is either null or real, i.e. not wild pointing to trash ...
c ++ null dynamic-cast
sdg Mar 01 2018-11-11T00: 00Z
source share