有时候,出于性能或可移植性的考虑,需要在iOS项目中使用到C++。
假设我们用C++写了下面的People类:
//
// People.h
// MixedWithCppDemo
//
// Created by Jason Lee on 12-8-18.
// Copyright (c) 2012年 Jason Lee. All rights reserved.
//
#ifndef __MixedWithCppDemo__People__
#define __MixedWithCppDemo__People__
#include <iostream>
class People
{
public:
void say(const char *words);
};
#endif /* defined(__MixedWithCppDemo__People__) */
//
// People.cpp
// MixedWithCppDemo
//
// Created by Jason Lee on 12-8-18.
// Copyright (c) 2012年 Jason Lee. All rights reserved.
//
#include "People.h"
void People::say(const char *words)
{
std::cout << words << std::endl;
}
然后,我们用Objective-C封装一下,这时候文件后缀需要为.mm,以告诉编译器这是和C++混编的代码:
//
// PeopleWrapper.h
// MixedWithCppDemo
//
// Created by Jason Lee on 12-8-18.
// Copyright (c) 2012年 Jason Lee. All r

本文介绍了如何在Xcode项目中使用C++与Objective-C混编,通过修改文件后缀为.mm,确保C++代码能够正确编译并运行。示例展示了在ViewController中调用C++类实现输出""Hello, Cpp.""的过程。"
112037439,10539402,Python算法效率:大O表示法与时间复杂度解析,"['Python', '算法', '时间复杂度', '数据结构', '大O表示法']
最低0.47元/天 解锁文章
328

被折叠的 条评论
为什么被折叠?



