我想知道,为什么不能有一个不是指针的 void 数据类型?
当然,您可以通过拥有
void4
void8
void32
然后只有在其大小等于或小于类大小时才允许将 void 数据类型“转换”到另一个类。
有什么我遗漏的东西,还是 C++ 委员会认为这是不好的做法?
编辑:
我没有很好地解释自己,所以我将举例说明它的使用:
main()
{
/*
Lets make a list of unknown elements
std::string is 8 bytes, and float is 4
bytes, so we'll reserve 8 byte sequences
*/
vector<void8> elements;
elements.push_back((void8) string("First string element"));
elements.push_back((void8) float(5.76) );
elements.push_back((void8) string("Third string element"));
// Ect.
cout << (string) elements[0];
cout << (float) elements[1];
cout << (string) elements[2];
cout << (float) elements[2]; // Garbage
void1 data;
data = (void1) bool(1);
data = (void1) unsigned int(80094); // Error, not enough size
}
它之所以命名为 void 是因为你不知道它当前存储的是什么类型,类似于 void 指针。