Search code examples
c++objective-cobjective-c++openframeworks

Calling variables from C++ class to Objective C class


I am creating an instance of an Objective C class from a C++ instance. The problem is that when trying to get the values of some vars (in the obj c instance) I always get 0. Some NSLogs are also ignored!:

Objective C class: 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 { public: 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";
}

and the result is always Finished? 0, even if I set it to 1 in purchase. Also the NSLog(@"STORE: HELLO THERE"); is ignored

I don't understand what's going on


Solution

  • In goToStoreFromMenu, you declare a new local variable named theStore. In checkPurchase, you refer to some other variable with the same name. The goToStoreFromMenu function initializes the local variable, which goes out of scope at the end of the function. You need to initialize the same variable that you'll eventually refer to in checkPurchase.