vc 与oc混编

来源:互联网 发布:hf线切割编程软件 编辑:程序博客网 时间:2024/06/01 20:51

步骤:

1.c++

[cpp] view plaincopyprint?
  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  

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


[cpp] view plaincopyprint?
  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封装

[cpp] view plaincopyprint?
  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  

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

[cpp] view plaincopyprint?
  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

[cpp] view plaincopyprint?
  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
原创粉丝点击