vc 与oc混编

步骤:

1.c++

  1. //  People.h  
  2. #ifndef __MXCPPTest__People__  
  3. #define __MXCPPTest__People__  
  4.   
  5. #include <iostream>  
  6. class People  
  7. {  
  8. public:  
  9.     void say(const char * words);  
  10. };  
  11. #endif  

-----------------


  1. //  
  2. //  People.cpp  
  3. //  MXCPPTest  
  4. //  
  5. //  Created by fengshaobo on 12-11-27.  
  6. //  Copyright (c) 2012年 fengshaobo. All rights reserved.  
  7. //  
  8.   
  9. #include "People.h"  
  10. void People::say(const char *words)  
  11. {  
  12.     std::cout << words << std::endl;  
  13. }  

2.oc封装

  1. //  
  2. //  Student.h  
  3. //  MXCPPTest  
  4. //  
  5. //  Created by fengshaobo on 12-11-27.  
  6. //  Copyright (c) 2012年 fengshaobo. All rights reserved.  
  7. //  
  8.   
  9. #import <Foundation/Foundation.h>  
  10. #import "People.h"  
  11.   
  12. @interface Student : NSObject  
  13. {  
  14.     People *p;  
  15. }  
  16.   
  17. - (void)say:(NSString *)words;  
  18.   
  19. @end  

-----------------

  1. //  
  2. //  Student.mm  
  3. //  MXCPPTest  
  4. //  
  5. //  Created by fengshaobo on 12-11-27.  
  6. //  Copyright (c) 2012年 fengshaobo. All rights reserved.  
  7. //  
  8.   
  9. #import "Student.h"  
  10.   
  11. @implementation Student  
  12. - (void)say:(NSString *)words  
  13. {  
  14.     p->say([words UTF8String]);  
  15. }  
  16. @end  


3.将.m -> .mm

  1. //  
  2. //  ViewController.mm  
  3. //  MXCPPTest  
  4. //  
  5. //  Created by fengshaobo on 12-11-27.  
  6. //  Copyright (c) 2012年 fengshaobo. All rights reserved.  
  7. //  
  8.   
  9. #import "ViewController.h"  
  10.   
  11. @interface ViewController ()  
  12.   
  13. @end  
  14.   
  15. @implementation ViewController  
  16.   
  17. - (void)viewDidLoad  
  18. {  
  19.     [super viewDidLoad];  
  20.       
  21.     Student *s = [[Student alloc] init];  
  22.     [s say:@"hello world!"];  
  23.     [s release];  
  24.     s = nil;  
  25. }  
  26.   
  27. - (void)didReceiveMemoryWarning  
  28. {  
  29.     [super didReceiveMemoryWarning];  
  30.     // Dispose of any resources that can be recreated.  
  31. }  
  32.   
  33. @end  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值