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

前端 未结 4 1225
陌清茗
陌清茗 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条回答
  •  梦毁少年i
    2021-02-04 05:35

    Neither, it's nullptr.

    Though, in your case, I'd just go with

    if ( !p ){
       //something
    }
    

    2.14.7 Pointer literals [lex.nullptr]

    1 The pointer literal is the keyword nullptr. It is a prvalue of type std::nullptr_t. [ Note: std::nullptr_t is a distinct type that is neither a pointer type nor a pointer to member type; rather, a prvalue of this type is a null pointer constant and can be converted to a null pointer value or null member pointer value.

提交回复
热议问题