Void是最容易解释的数据类型,他的意思是“没有类型”。因此变量不能定义为void类型。
void value; // won't work, variables can't be defined with a void type
void通常用于几种不同的上下文中。
1.最常用的适用于一个函数不返回任何返回值的情况
void writeValue(int x) // void here means no return value
{
std::cout << "The value of x is: " << x << std::endl;
// no return statement, because the return type is void
}
2.函数参数里表示该函数没有任何参数
int getValue(void) // void here means no parameters
{
int x;
std::cin >> x;
return x;
}
这种用法在C语言中,当然不会在C++中使用,C++中使用的是()如下:
int getValue() // empty function parameters is an implicit void
{
int x;
std::cin >> x;
return x;
}
3.在后续的6.13节我们还会学习一种void的使用方法,void pointer