When I try to study QP/CPP code I came across below line.
当我尝试研究QP/CPP代码时,我遇到了下面一行。
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?
为什么他们做的是static_cast (0) ?如果他们想检查NULL我们可以直接做,对吧?
This source code you can find out in
这是你能找到的源代码。
http://www.state-machine.com/qpcpp/qf__time_8cpp_source.html
http://www.state-machine.com/qpcpp/qf__time_8cpp_source.html
2 个解决方案
#1
10
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.
当然,现在我们只要写nullptr就行了。
#2
4
The idiomatic way to write
习惯写法
QTimeEvt *t;
// ...
if (t == static_cast<QTimeEvt *>(0)) {
… is
…
QTimeEvt* t;
// ...
if( !t ) {
Or you can write it as
或者你可以把它写成。
if( not t ) {
… although you can also write
虽然你也可以写作
if( t == nullptr ) {
… or, ¹C++03-style,
……或者,¹c++ 03-style,
if( t == 0 ) {
There's no need for the cast.
不需要演员阵容。
It's just verbiage consuming the reader's time.
这只是在浪费读者的时间。
Notes:
¹ If the <stddefs.h>
header is included one can write NULL
instead of 0
where a nullpointer is required. With a modern implementation NULL
might even be defined as nullptr
.
注:如果< stddefs¹。h>标头包含一个可以写入NULL而不是0的空指针。使用现代实现,NULL甚至可以定义为nullptr。
#1
10
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.
当然,现在我们只要写nullptr就行了。
#2
4
The idiomatic way to write
习惯写法
QTimeEvt *t;
// ...
if (t == static_cast<QTimeEvt *>(0)) {
… is
…
QTimeEvt* t;
// ...
if( !t ) {
Or you can write it as
或者你可以把它写成。
if( not t ) {
… although you can also write
虽然你也可以写作
if( t == nullptr ) {
… or, ¹C++03-style,
……或者,¹c++ 03-style,
if( t == 0 ) {
There's no need for the cast.
不需要演员阵容。
It's just verbiage consuming the reader's time.
这只是在浪费读者的时间。
Notes:
¹ If the <stddefs.h>
header is included one can write NULL
instead of 0
where a nullpointer is required. With a modern implementation NULL
might even be defined as nullptr
.
注:如果< stddefs¹。h>标头包含一个可以写入NULL而不是0的空指针。使用现代实现,NULL甚至可以定义为nullptr。