void*和NULL和nullptr
void *可以表示任何的指针类型,比如我不知道未来传入的这个指针是什么类型的,那么我可以使用void *来表示未来传入的指针的类型
在C++中NULL和nullptr是不同的两个东西,他们是不能画上等于号的
在这里就可以看出来结果
#include<iostream>
using namespace std;
int main() {
int a = NULL;
cout << a << endl;
return 0;
}
这里定义了两个函数func,带入不同的参数分别调用函数,观察结果
#include<iostream>
using namespace std;
void func(void* ptr) {
cout << "This is void* func" << endl;
}
void func(int i) {
cout << "This is int func" << endl;
}
int main() {
int* ptr1 = NULL;
int* ptr2 = nullptr;
char* ptr3 = NULL;
char* ptr4 = nullptr;
int a = NULL;
int b = 0;
func(ptr1);
func(ptr2);
func(ptr3);
func(ptr4);
func(a);
func(b);
return 0;
}
ptr3);
func(ptr4);
func(a);
func(b);
return 0;
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/c5ddaea3017c4a9ea95ae7cabedd1715.png)