1. Hello.mm
* 程序见后
* console中build
g++ -x objective-c++ -framework Foundation Hello.mm -o hello
OK.
*Xcode build
Error:
ld: duplicate symbol _main in /Users/frankzou/study/HelloObjectiveCPP/build/HelloObjectiveCPP.build/Debug/HelloObjectiveCPP.build/Objects-normal/x86_64/Hello.o and /Users/frankzou/study/HelloObjectiveCPP/build/HelloObjectiveCPP.build/Debug/HelloObjectiveCPP.build/Objects-normal/x86_64/main.o
* source code
//
// Hello.mm
// HelloObjectiveCPP
//
// Created by frank.zou on 12-2-9.
// Copyright 2012 __MyCompanyName__. All rights reserved.
//
/* Hello.mm
* Compile with: g++ -x objective-c++ -framework Foundation Hello.mm -o hello
*/
#import <Foundation/Foundation.h>
class Hello {
private:
id greeting_text; // holds an NSString
public:
Hello() {
greeting_text = @"Hello, world!";
}
Hello(const char* initial_greeting_text) {
greeting_text = [[NSString alloc] initWithUTF8String:initial_greeting_text];
}
void say_hello() {
printf("%s\n", [greeting_text UTF8String]);
}
};
@interface Greeting : NSObject {
@private
Hello *hello;
}
- (id)init;
- (void)dealloc;
- (void)sayGreeting;
- (void)sayGreeting:(Hello*)greeting;
@end
@implementation Greeting
- (id)init {
if (self = [super init]) {
hello = new Hello();
}
return self;
}
- (void)dealloc {
delete hello;
[super dealloc];
}
- (void)sayGreeting {
hello->say_hello();
}
- (void)sayGreeting:(Hello*)greeting {
greeting->say_hello();
}
@end
int main() {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
Greeting *greeting = [[Greeting alloc] init];
[greeting sayGreeting]; // > Hello, world!
Hello *hello = new Hello("Bonjour, monde!");
[greeting sayGreeting:hello]; // > Bonjour, monde!
delete hello;
[greeting release];
[pool release];
return 0;
}