两次取反(!!)的作用

作用

将指定内容转换为bool处理,true或者false
如果不存在bool类型,则转换为1或者0处理,不存在其他数字,不存在其他数字,不存在其他数字

举例

  1. 如果为bool值,则两次取反后结果不变
    bool a1 = true;
    bool b1 = !!a1;  // true

    bool a2 = false;
    bool b2 = !!a1;  // false
  1. 如果为in值,则两次取反后结果为true或者false(也就是1或者0)
    目前本人仅遇到过这种情况
    int  a3 = 3;
    bool b3 = !!a3;  // true 

    int  a4 = 0;
    bool b4 = !!a4;  // false 
  1. 如果为指针类型,则为判断指针是否为空
    int* a9 = new int();
    bool b9 = !!a9;  // true

    int a10 = NULL;
    bool b10 = !!a10; // false;
  1. 如果为字符串类型
    const char* a5  = "abc";
    bool        b5  = !!a5;     // true
    bool        b55 = !!(*a5);  // true

    const char* a6  = "";
    bool        b6  = !!a6;     // true
    bool        b66 = !!(*a6);  // false
  1. 如果为字符类型,则两次取反后全部为true
    char a7 = 'a';
    bool b7 = !!a7;  // true

    char a8 = ' ';
    bool b8 = !!a8;  // true
posted @ 2022-02-09 17:56  flxx  阅读(808)  评论(0编辑  收藏  举报