zoukankan      html  css  js  c++  java
  • c++ nullptr

    nullptr使用代码如下:

    int *ptr = nullptr;

    大家对此有过疑问吗,都是表示空指针,之前NULL使用的好好的,为什么要引入nullptr,nullptr和NULL又有什么区别呢? 首先看下NULL究竟是什么? 源码在此:

    #ifndef NULL
        #ifdef __cplusplus
            #define NULL 0
        #else
            #define NULL ((void *)0)
        #endif
    #endif

    从源码中可以知道,在C中NULL是((void *)0)指针,在C++中NULL却是个整数0。

    这里C++ 中的NULL如果和C语言一样也是(void )0指针,而C++ 却又不允许void隐式转换成其它指针类型,那还怎么用NULL来表示空指针呢,岂不是尴尬了。

    下面代码编译会报错:

    #include <iostream>
    
    int main() {
        int *a = (void *)0;
        return 0;
    }
    error: cannot initialize a variable of type 'int *' with an rvalue of type 'void *'

    再来看看究竟为什么要引入nullptr?
    一个原因是可以让整形0放下重担,0只表示一件事情,它只是一个整数类型0,没有任何其它语义,空指针的活就安排给其它员工,这个员工就是nullptr关键字。

    先看一段代码:

    void func(char*) {
        cout << "char*";
    }
    void func(int) {
        cout << "int";
    }
    
    int main() {
         func(NULL); // 编译失败 error: call of overloaded ‘func(NULL)’ is ambiguous
        func(nullptr); // char*
        return 0;
    }

     

    #include <iostream>
    using namespace std;
    
    void myprint(char* p) {
        std::cout << "pointer
    ";
    }
     
    void myprint(int n) {
        std::cout << "integer
    ";
    }
    
    int main() {
        myprint(0);
        myprint(NULL); // compile error
        return 0;
    }
    root@ubuntu:~/c++# g++   -std=c++11  null.cpp -o null
    null.cpp: In function ‘int main()’:
    null.cpp:14:17: error: call of overloaded ‘myprint(NULL)’ is ambiguous
         myprint(NULL); // compile error
                     ^
    null.cpp:4:6: note: candidate: void myprint(char*)
     void myprint(char* p) {
          ^
    null.cpp:8:6: note: candidate: void myprint(int)
     void myprint(int n) {
          ^
    #include <iostream>
    using namespace std;
    
    void myprint(char* p) {
        std::cout << "pointer
    ";
    }
     
    void myprint(int n) {
        std::cout << "integer
    ";
    }
    
    int main() {
        myprint(0);
        myprint((char*)NULL); 
        myprint(nullptr); 
        return 0;
    }
    root@ubuntu:~/c++# g++   -std=c++11  null.cpp -o null
    root@ubuntu:~/c++# ./null 
    integer
    pointer
    pointer

    nullptr为什么可以转换成任何指针类型

    struct nullptr_t
    {
        void operator&() const = delete;  // Can't take address of nullptr
    
        template <class T>
        inline operator T*() const
        {
            return 0;
        }
    
        template <class C, class T>
        inline operator T C::*() const
        {
            return 0;
        }
    };
    
    nullptr_t nullptr;

    nullptr如何使用

    nullptr关键字用于标识空指针,是std::nullptr_t类型的(constexpr)变量。它可以转换成任何指针类型和bool布尔类型(主要是为了兼容普通指针可以作为条件判断语句的写法),但是不能被转换为整数。

    char *p1 = nullptr;     // 正确
    int  *p2 = nullptr;     // 正确
    bool b = nullptr;       // 正确. if(b)判断为false
    int a = nullptr;        // error
  • 相关阅读:
    hdu 1028 Ignatius and the Princess III (n的划分)
    CodeForces
    poj 3254 Corn Fields (状压DP入门)
    HYSBZ 1040 骑士 (基环外向树DP)
    PAT 1071 Speech Patterns (25)
    PAT 1077 Kuchiguse (20)
    PAT 1043 Is It a Binary Search Tree (25)
    PAT 1053 Path of Equal Weight (30)
    c++ 常用标准库
    常见数学问题
  • 原文地址:https://www.cnblogs.com/dream397/p/15076439.html
Copyright © 2011-2022 走看看