When I try to study QP/CPP code I came across below line.
QTimeEvt *t;
// ...
if (t == static_cast<QTimeEvt *>(0)) {
Why they are doing static_cast of 0? If they want to check NULL we can do that directly right?
This source code you can find out in
http://www.state-machine.com/qpcpp/qf__time_8cpp_source.html
The static_cast operator can be used for operations such as converting a pointer to a base class to a pointer to a derived class. Such conversions are not always safe.
No. If the pointer refers to a valid object, and the conversion is valid, then the result will also refer to a valid object, so it won't be null.
static_cast can be used to convert between pointers to related classes (up or down the inheritance hierarchy). It can also perform implicit conversions.
You shouldn't use static_cast for casting down an inheritance hierarchy, but rather dynamic_cast . That will return either the null pointer or a valid pointer.
Yeah, that's unnecessary, though it may be mandated by some style guide for "clarity", or it may be there to silence an overzealous static analysis tool.
Of course, nowadays, we'd just write nullptr
and leave it at that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With