nullptr关键字就是表示空指针的意思,C++11之前空指针都是NULL。
已经有了NULL,为什么还要有nullptr关键字?
先来看下传空指针的例子:
|
1 2 3 4 5 6 |
#include <stddef.h> void fun(int) {} // #1 void fun(char*) {} // #2 int main() { fun(NULL); // 调用#1还是#2? } |
这样写会调用fun(int)函数,因为NULL只是个宏定义,值是0是int类型。
我们希望调用的是fun(char*)哪怎么办?改成如方式:
|
1 2 3 4 5 6 7 |
#include <stddef.h> void fun(int) {} void fun(char*) {} int main() { char* cp = NULL fun(cp ); } |
但在C++11中还有更好的解决办法,哪就是用nullptr关键字
|
1 2 3 4 5 6 |
#include <stddef.h> void fun(int) {} void fun(char*) {} int main() { fun(nullptr); } |
用nullptr关键字处理这样问题,代码更简洁、更高效。
注:nullptr表示空指针,不能转换为整型类型。为了向后兼容,C++11仍允许用0(NULL)来表示空指针。建议使用nullptr而不是0(NULL),因为更高的类型安全。
You actually reported it exceptionally well. canadian pharmacies that ship to us
This is nicely said! . canadianpharmacy
Nicely put, Kudos! canadian drugs
Thanks a lot. A good amount of postings!
ed meds online without doctor prescription
Many thanks! A good amount of advice.
Order Propecia Without A Doctor Prescription
Many thanks, Quite a lot of knowledge!
is cbd oil legal in florida
Valuable knowledge. Regards! https://viagradjango.com/
Good advice. Cheers! glycomet
Appreciate it! Quite a lot of posts!
Escitalopram 30 Mg