views:

127

answers:

2

// --------------------------------------------------------------------

UPDATE: I messed up. I left a call directly to openfeint in my AppDelegate->onApplicationWillResignActive which was causing the compiler C++ error.

My Appologies, the singleton does work should anyone be thinking of trying the same thing. Just be sure to include the header in the .m file and not the header file.

// --------------------------------------------------------------------

I'm building an iPhone app, and using the Openfeint SDK/Library/Framework (??) which is written in C++.

I'm wondering, is it possible to write a class to interface with C++ so that I don't have to change my ObjC classes to .mm files.

I've tried creating a singleton, in the hope that I could include it's header in normal .m files, but that doesn't work, I still need to make the file that includes the header .mm

The reason I want to do this (or something like this) is because I've no experience with C++, and changing ObjC to C++ files has been causing errors and warnings.

Here's the singleton I created...

// --------------------------------------------------------------------
//  OpenfeintController.h
// --------------------------------------------------------------------
#import <Foundation/Foundation.h>

@interface OpenfeintController : NSObject {

    NSString *productKey, *secretKey, *displayName;

}

+(OpenfeintController*)sharedOpenfeintController;

- (void) initializeWithProductKey:(NSString *)pKey andSecretKey:(NSString *)sKey andDisplayName:dName;

- (void) launchOpenFeint;

- (void) submitHighScoreToLeaderboard:(NSString *)leaderboardId;

@end

Implmentation

// --------------------------------------------------------------------
//  OpenfeintController.mm
// --------------------------------------------------------------------

#import "OpenfeintController.h"
#import "OpenFeint.h"

static OpenfeintController *singletonOpenfeintController = nil;

@implementation OpenfeintController

+(OpenfeintController*)sharedOpenfeintController {
    @synchronized(self) {
     if (!singletonOpenfeintController) {
      singletonOpenfeintController = [[OpenfeintController alloc] init];
     }
    }
    return singletonOpenfeintController;
}



- (void) initializeWithProductKey:(NSString *)pKey andSecretKey:(NSString *)sKey andDisplayName:dName
{
    //[OpenFeint initializeWithProductKey:pKey andSecret:sKey andDisplayName:dName andSettings:nil andDelegates:nil];
}

- (void) launchOpenFeint
{

}

- (void) submitHighScoreToLeaderboard:(NSString *)leaderboardId
{

}

@end
A: 

I do something similar with a different C++ library (GeographicLib) and it works without problems. Just make sure that no C++ code or imported C++ headers are in your .h file. I don't see anything wrong with your code, though, so I'm afraid I can't really help you. I have no experience with OpenFeint.

Ole Begemann
+2  A: 

This is certainly possible to do, unfortunately you’ve not really given any information that could help us determine what problems you are having. You say that users of the OpenfeintController.h header need to be Objective-C++, but there doesn’t appear to be any C++ included in the header you’ve posted (so this shouldn’t be necessary).

If the compilation is giving errors when you do this, then post the errors so we can see what is actually happening.

Ciarán Walsh