http://download.csdn.net/source/2803162
已经将完整的工程上传到csdn的资源中了,手动贴出链接。
如有其它的交流,欢迎相互交流。
看下本blog后续贴出的转载的那篇翻译的官方文档,跑一下我给的这个例子,混编应该没什么大碍了吧,自吹一下,哈哈哈。
iphone开发里面xcode使用stl其实也不是有那么困难的,我例子只是基本的使用方式说明,高级的还要自己努力。
废话不多,代码放出。
不知道怎么上传附件,放到资源里面自己去下吧。
//单独的一个c++类,和普通的写法没什么两样
新建一个工程OCplusplus,将头文件和m文件改成如下内容,记得要改名成mm文件。
#import <UIKit/UIKit.h>
@interface OCplusplusAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
NSString* test();
@end
#import "OCplusplusAppDelegate.h"
#import "cplusplus.h"
#import "hellocpp.h"
#import "useSTL.h"
//这个文件就是要用到c++的东西,就要改后缀名
@implementation OCplusplusAppDelegate
@synthesize window;
//试试,自己用自己知道。
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Override point for customization after application launch
[window makeKeyAndVisible];
cplusplus *cpp = new cplusplus();
NSLog(@"%d",cpp->testadd(4, 6));
delete cpp;
hello *h = new hello();
NSLog(@"%d",h->testadd(11, 22));
h->sayhello();
NSLog(@"%@",h->OCsayello());
NSLog(@"%@",test());
delete h;
useSTL *us = new useSTL();
us->testSTL();
delete us;//干什么都要干净利落,不要拉下东西,记得释放内存
}
NSString* test()
{//这就是oc类的c++写法
NSLog(@"jhdglajdklfja");
return @"ttttttttt";
}
- (void)dealloc {
[window release];
[super dealloc];
}
@end
//////////////////////////////////////////////////
新建一个文件cplusplus,也就是一个类,也是改名成mm文件
class cplusplus
{
public:
int testadd(int a, int b);
};
#import "cplusplus.h"
int cplusplus::testadd(int a,int b)
{
return a + b;
}
//////////////////////////////////////////////////
再新建一个文件hellocpp ,也是一个内嵌类,也需要改成mm文件
#import <Foundation/Foundation.h>
#import "cplusplus.h"
@interface hellocpp : NSObject {
//这是内嵌类,自己试试自己知道
class hello : public cplusplus {
public:
void sayhello();
NSString *OCsayello();
};
}
@end
#import "hellocpp.h"
#import "cplusplus.h"
@implementation hellocpp
void hello::sayhello()
{
printf("say hello in hello");
}
NSString* hello::OCsayello()
{//这种写法也可以,就会在oc类里也可以的。
NSLog(@"OC say hello");
return @"test";
}
@end
////////////////////////////////////////////
再新建一个文件useSTL,也是一个类,也需要改成mm文件
这回要使用的是c++中的stl的vector功能
//内嵌的类也是可以的
class useSTL {
public:
void testSTL();
};
//////////////////////////////////////////
#import "useSTL.h"
using namespace std;//这个也要写上才行
#import "vector"//记得头文件啊
#include <iostream>
void useSTL::testSTL()
{//这段代码是网上随意copy的,有点修改。
int n = 9;
vector<int>* vectors = new vector<int>[n];
int i;
for(i=0; i<n; i++)
{
for(int j=0; j<i; j++)
{
int data;
cin>>data;
vectors[i].push_back(j);
}
}
cout<<"共有"<<n<<"个vector,各vector元素如下:"<<endl;
for(i=0; i<n; i++)
{
cout<<"第"<<i+1<<"个vector的元素:";
int j;
for(j=0; j<vectors[i].size(); j++)
{
cout<<vectors[i][j]<<"/t";
}
cout<<endl;
}
delete [] vectors;
}
确保工程可以编译通过,看一下运行的结果。
运行结果:
[Session started at 2010-11-02 15:56:56 +0800.]
2010-11-02 15:56:58.345 OCplusplus[10303:207] 10
2010-11-02 15:56:58.350 OCplusplus[10303:207] 33
say hello in hello2010-11-02 15:56:58.353 OCplusplus[10303:207] OC say hello
2010-11-02 15:56:58.354 OCplusplus[10303:207] test
2010-11-02 15:56:58.354 OCplusplus[10303:207] jhdglajdklfja
2010-11-02 15:56:58.355 OCplusplus[10303:207] ttttttttt
共有9个vector,各vector元素如下:
第1个vector的元素:
第2个vector的元素:0
第3个vector的元素:0 1
第4个vector的元素:0 1 2
第5个vector的元素:0 1 2 3
第6个vector的元素:0 1 2 3 4
第7个vector的元素:0 1 2 3 4 5
第8个vector的元素:0 1 2 3 4 5 6
第9个vector的元素:0 1 2 3 4 5 6 7
分享到:
相关推荐
C++和Objective-C混编 如何在xcode中用C++的STL 这是一个工程例子。我的blog有提到。
自己在Xcode4下编写的Objective-C和C语言混编的计算器
苹果的Objective-C编译器允许用户在同一个源文件里自由地混合使用C++和Objective-C,混编后的语言叫Objective-C++。有了它,你可以在Objective-C应用程序中使用已有的C++类库。 Objective-C和C++混编的要点 ...
《Objective-C 程序设计(第4版)》已经为iOS 5和Xcode4.2中的重大变更做了全面更新,最大的改动是引入了自动引用计数(ARC),并详细说明了如何在Objective-C编程过程中使用ARC提升和简化内存管理。
Objective-C是扩展C的面向对象编程语言,也是iPhone开发用到的主要语言。本书结合理论知识与示例程序,全面而系统地讲述Objective-C编程的相关内容,包括Objective-C在C的基础上引入的特性和Cocoa工具包的功能及其中...
网上看到一些混合编程的文章,总是遇到一些问题。经过半天时间的摸索,自己终于可以顺利完成啦。以下步骤中,Hello类只是为Objective-C调用C++做的一个封装。讲解一下操作步骤.
iOS 7 Programming Fundamentals: Objective-C, Xcode, and Cocoa Basics by Matt Neuburg (Author) Publisher: O’Reilly Media (October 2013) Language: English ISBN-10: 1491945575 ISBN-13: 978-...
Objective-C使用换行符来实现多行输出
Cocoa多线程开发Objective-C详解 多线程 Objective-C iOS cocoa Xcode
Pro Objective-C The Objective-C programming language continues to grow in popularity and usage because of the power and ease-of-use of the language itself, along with the numerous features that ...
Objective-C 分离 接口和实现文件,具体资料请参考苹果开发者Mike的博文http://blog.sina.com.cn/s/blog_7aa21f320100r7wn.html
从C++到Objective C 非常适合刚接触IOS开发的初学者 学习
xcode调用c++代码 如何在xcode中用C++的STL(源代码放出)C++和Objective-C混编
Objective-C是Apple软件的编程语言,在学习、调试之前,需要搭建一个集成开发环境(IDE)。一般针对Objective-C的IDE开发环境的搭建方式有三类方法: 1. 拥有Apple平台(即苹果电脑一台),集成开发环境使用Xcode; ...
Xcode 使用技巧 Objective-C学习之路 iphone
If you want to learn Objective-C 2.0 to write programs for Mac OS X, iPhone, iPad, or iPod touch, you’ve come to the right place! Concise, readable, and friendly, Learning Objective-C 2.0 is the ...
通过《Objective-C开发范例代码大全》成为开发专家!本书采用“先提出问题,后提 供解决方案”的方式讲解Objective-C编程中的核心内容,是iOS开发人员手中不可或缺的 参考指南。 本书作者Matthew Campbell在培训...
Objective-C学习之路 使用Xcode
Swift和Objective-C 混编注意事项整理: 前言 Swift已推出数年,与Objective-C相比Swift的语言机制及使用简易程度上更接地气,大大降低了iOS入门门槛。当然这对新入行的童鞋没来讲,的确算是福音,但对于整个iOS...
Learn Objective-C for Java Developers Learn Objective-C for Java Developers will guide experienced Java developers into the world of Objective-C. It will show them how to take their existing language...