有时候,出于性能或可移植性的考虑,需要在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
//