从C ++类调用变量到Objective C类

时间:2011-03-13 20:02:22

标签: c++ objective-c objective-c++ openframeworks

我正在从C ++实例创建Objective C类的实例。问题是,当试图获取某些变量的值时(在obj c实例中)我总是得到0.有些NSLog也被忽略了!:

目标C类: InAppPurchaseManager.h

@interface InAppPurchaseManager : NSObject <SKProductsRequestDelegate, SKPaymentTransactionObserver>{
    SKProduct *proUpgradeProduct;
    SKProductsRequest *productsRequest;
@public
    int finishedPurchaseProcess;
}
- (int)hasFinishedPurchaseProcess;
- (void)purchase;
@end

InAppPurchaseManager.m

@implementation InAppPurchaseManager
- (void)purchase{
    finishedPurchaseProcess=1;
}
- (int)hasFinishedPurchaseProcess{
    NSLog(@"STORE: HELLO THERE");   
    return finishedPurchaseProcess;
}

testApp.h     class testApp:public ofxiPhoneApp {         上市:         void goToStoreFromMenu();         void checkPurchase();         InAppPurchaseManager * theStore;     }

testApp.mm

// First I call ghis function
void testApp::goToStoreFromMenu(){  
    InAppPurchaseManager* theStore = [[InAppPurchaseManager alloc] init];
    [theStore purchase];
}

// And then this function
void testApp::checkPurchase(){  
    cout << "Finished? " << [theStore hasFinishedPurchaseProcess] << "\n";
}

结果总是完成? 0 ,即使我在购买中将其设置为1。此外 NSLog(@“STORE:HELLO THERE”); 被忽略

我不明白发生了什么

1 个答案:

答案 0 :(得分:1)

goToStoreFromMenu中,声明一个名为theStore的新局部变量。在checkPurchase中,您引用了一些具有相同名称的其他变量。 goToStoreFromMenu函数初始化局部变量,该变量在函数末尾超出范围。您需要初始化最终在checkPurchase中引用的相同变量。