我有一個函數返回double
在void *
。但是,它似乎鬆了指針或似乎無法獲得該值,因爲它返回0一次的功能。在void中存儲雙精度*
你將如何創建一個double並將它賦值爲void *的值,然後在不超出範圍的情況下將其返回。
此功能的想法是將一個對象轉換的Objective-C的東西在C++,但在C++中不是所有的東西都是對象,如雙,它是客觀C.
void * IDToSTD (id anObject){
void * stdObject = NULL;
if ([anObject class] == [PFObject class]) {
stdObject = BridgePFObject((void *)CFBridgingRetain(anObject));
return stdObject;
}
CFTypeRef inTypeRef = (__bridge CFTypeRef)anObject;
CFTypeID type = CFGetTypeID(inTypeRef);
if (type == CFArrayGetTypeID()) {
std::vector<void *> arr = NSArrayToSTDVector([(NSArray *)anObject copy]);
stdObject = &arr;
} else if (type == CFDictionaryGetTypeID()) {
std::map<std::string, void *> dict = NSDictionaryToSTDMap([(NSDictionary *)anObject copy]);
stdObject = &dict;
} else if (type == CFStringGetTypeID()) {
stdObject = (char *)[(NSString *) anObject cStringUsingEncoding:NSUTF8StringEncoding];
} else if (type == CFNumberGetTypeID()) {
double *value = (double *)malloc(sizeof(double *));
*value = [(NSNumber *)anObject doubleValue];
stdObject = value;
} else {
[NSException raise:@"Invalid object value" format:@"Object must be of basic Core Foundation or Parse Type, Object Type is: %@", NSStringFromClass([anObject class])];
}
return stdObject;
}
爲什麼不只是返回一個'double'? –
是否將堆的值存儲在堆或棧上? (使用'new'操作符還是不行?) – Matthew
你能顯示代碼嗎?你正在編譯64位或32位?這聽起來像嘗試做生意的可怕方式。這聽起來好像你可能會嘗試:'void * vp;雙d = 3.14159; *(double *)&vp = d;'或者同樣野蠻的東西。 –