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 {
- @private
- Hello *hello;
- }
-
- - (id)init;
- - (void)dealloc;
- - (void)sayGreeting;
- - (void)sayGreeting:(Hello *)greeting;
- @end
Greeting.mm
- #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結尾,否則無法編譯。
免費下載地址在 http://linux.linuxidc.com/
用戶名與密碼都是www.linuxidc.com
具體下載目錄在 /2012年資料/9月/2日/在Objective-C中使用C++