zoukankan      html  css  js  c++  java
  • 初始化指针

    #include<iostream>
    using namespace std;
    int main()
    {
    	int *a(nullptr), *b(NULL), *c(0);
    	if(!a) cout<<" 'a' does not point to anything. 
    ";
    	if(!b) cout<<" 'b' does not point to anything. 
    ";
    	if(!c) cout<<" 'c' does not point to anything. 
    ";
    	return 0;
    }

    nullptr C++新标准引入的特性, Visual C++ 2010 编译器支持它。过去已经使用0NULL(编译器将用0代替此宏)来初始化指针,当然它们现在仍然可以使用。但是,使用nullptr初始化指针要好得多。

    因为字面值nullptr可以隐式转换为bool类型,所以我们可以如下面这样来检查指针的状态:

    if(!a) 

    cout<<" 'a' does not point to anything.  "; 

    nullptr转换为boolfalse,其他任何指针值都可以为true


  • 相关阅读:
    shell 参数个数
    小坑也难受
    MaHua简介
    airflow Operators
    datax
    T-SQL 更新表操作
    T-SQL时间函数
    linux学习网站
    好看的页面
    函数-1
  • 原文地址:https://www.cnblogs.com/Genesis2018/p/8304773.html
Copyright © 2011-2022 走看看