Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

imaxx

macrumors newbie
Original poster
Aug 13, 2009
5
0
hello,

I have a slight problem with MAC (well, iphone target, but this is a more general problem) and Obj c++ (I am at the sea atm so i have some problem compiling).
I have the following code:
Code:
           static NSString* uniqueID = nil;
           const char *pID;
	uniqueID = [[UIDevice currentDevice] uniqueIdentifier];
	uniqueID = [[[uniqueID uppercaseString] stringByReplacingOccurrencesOfString:@":" withString:@"-"] copy];
	pID = [uniqueID UTF8String];
and my problem is: will this code run if included WITHIN a ".cpp" file? What do I need to do to make it execute?

Thanks!
 
You can only use Objective-C in either pure Objective-C or Objective-C++ files. So .m or .mm files only. A cpp file will be treated as pure C++ and cannot, therefore, contain Objective-C syntax.
 
hello,

I have a slight problem with MAC (well, iphone target, but this is a more general problem) and Obj c++ (I am at the sea atm so i have some problem compiling).
I have the following code:
Code:
           static NSString* uniqueID = nil;
           const char *pID;
	uniqueID = [[UIDevice currentDevice] uniqueIdentifier];
	uniqueID = [[[uniqueID uppercaseString] stringByReplacingOccurrencesOfString:@":" withString:@"-"] copy];
	pID = [uniqueID UTF8String];
and my problem is: will this code run if included WITHIN a ".cpp" file? What do I need to do to make it execute?

Thanks!

Objective-C++ is a superset of C++, just as Objective-C is a superset of C. In XCode, you could select the .cpp file, "Get Info" and change it from "C++ source file" to "Objective-C++ source file". It would be less confusing to the rest of the world if you just create a .mm file. In a .mm file, you can freely mix Objective-C and C++ code.
 
I was thinking to split the .cpp file, by adding a function that calls the code in a .mm file. However, both will use the same file header for prototypes.
This should add the needed clarity, i suppose.

Would you think this solution would be deemed as 'appropriate'? Basically I make a c function with such code, and make the prototype available on the same .h file. The cpp file will call such function kept in the .mm file.

I suppose this would work fine (.cpp calling .mm), am I wrong?

Thanks for your answers, btw :)
 
Is there a need to use the cpp file elsewhere (in another project) that is C++-only? If not, why not just put everything into a .mm file and be done with it? Compiling something in Objective-C++ mode will do the right thing with plain C++. If you want to split things up for logical reasons, and not "language" reasons, that's perfectly fine, and using a header file for prototypes is the right thing to do. But even if you do divide them up, the cpp file can still be a mm file in case you wanted to add some Objective-C method invocations in the future, etc.

-Lee
 
well, it is part of a multi-platform code. Basically I need to compile it on various platforms.

I could just keep it into a c++ file, and request the whole code to be added as Objc++, but probably if i split it up, it will be more clear to me, at least from a 'visual' standpoint in the project.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.