首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

在Objective-C中应用C++

2012-09-27 
在Objective-C中使用C++Greeting.h#import Foundation/Foundation.hclass Hello {private:id greeting_t

在Objective-C中使用C++

Greeting.h

#import <Foundation/Foundation.h>class Hello {private:id greeting_text;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 {@privateHello *hello;}- (id)init;- (void)dealloc;- (void)sayGreeting;- (void)sayGreeting:(Hello *)greeting;@end?

#import "Greeting.h"@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

Greeting *greeting = [[Greeting alloc] init];[greeting sayGreeting]; Hello *hello = new Hello("Hello");[greeting sayGreeting:hello]; delete hello;[greeting release];

?

主要是要注意类必须以mm结尾,否则无法编译。

热点排行