在c++11中,nullptr可完全代替NULL.  然而NULL和nullptr还是稍有不同,NULL可被转化为int类型,而nullptr不能。因此nullptr对NULL在进行模板推导或者函数重载时的缺陷。

#include <iostream>

void foo(int x)
{
  std::cout << "int" << std::endl;
}

void foo(int* x)
{
  std::cout << "int*" << std::endl;
}

int main()
{
  foo(nullptr);
  foo(NULL);
  return 0;
}

  foo(nullptr)输出"int*", foo(NULL)输出"int"

  

相关文章:

  • 2022-12-23
  • 2021-10-24
  • 2021-12-02
  • 2022-01-11
  • 2022-02-09
  • 2022-12-23
  • 2022-12-23
  • 2021-05-20
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-12-17
  • 2021-07-18
  • 2021-08-04
  • 2022-01-29
  • 2021-06-07
相关资源
相似解决方案