我实际的问题是,真的有可能比较值当你真正知道这些值是相同类型包含在两个空指针,?例如int.
void compVoids(void *firstVal, void *secondVal){
if (firstVal < secondVal){
cout << "This will not make any sense as this will compare addresses, not values" << endl;
}
}
Run Code Online (Sandbox Code Playgroud)
实际上我需要比较两个void指针值,而在函数外部,已知类型为int.我不想在函数内部使用int的比较.所以这对我也不起作用:有if (*(int*)firstVal > *(int*)secondVal)
什么建议吗?非常感谢您的帮助!
我有下一个问题.我用int memcmp ( const void * ptr1, const void * ptr2, size_t num );
函数来比较两个包含整数的void指针.这对我很有用.
int firstValue = 5;
int secondValue = 3;
void* firstValueVoid;
void* secondValueVoid
firstValueVoid = new int(firstValue);
secondValueVoid = new int(secondValue);
int compare = memcmp(firstValueVoid, secondValueVoid, 4);
cout << compare << endl;
Run Code Online (Sandbox Code Playgroud)
但是,如果我想对字符串进行同样的操作,它总是表明第一个值小于第二个值.
string firstValue = "abc";
string secondValue = "a";
int testSize = firstValue.length();
void* firstValueVoid;
void* secondValueVoid
firstValueVoid = new string(firstValue);
secondValueVoid = new string(secondValue);
int compare = memcmp(firstValueVoid, secondValueVoid, testSize);
cout << …Run Code Online (Sandbox Code Playgroud)