Whats better to use in C++11 , Zero or NULL?

前端 未结 4 1288
陌清茗
陌清茗 2021-02-04 05:19

Nowadays, with C++11, Whats recommended to use, Zero or NULL? The first of the second if?

int * p = getPointer();

if( 0 == p ){
    //         


        
4条回答
  •  眼角桃花
    2021-02-04 05:28

    C++11 has a new literal keyword nullptr. It's better than 0 or NULL for things like this because there's no chance it will be used as an int in overload resolution.

    if ( nullptr == p )
    

    Or of course you can just use a pointer in a bool context:

    if ( !p )
    

提交回复
热议问题