码迷,mamicode.com
首页 > 编程语言 > 详细

oc中如何调用c++的方法

时间:2014-07-10 21:46:37      阅读:353      评论:0      收藏:0      [点我收藏+]

标签:c++与oc交互   oc调用c++方法   oc中如何调用c++的方法   c++   oc   

有的时候,我们需要调用纯c++的方法,这个时候,我们必须再次封装一下。通过调用中间层对象的方法,来调用c++的方法。请看下图:

bubuko.com,布布扣

2.在test.h文件中定义方法

#ifndef __test__
#define __test__

class Test
{
public:
    void test();
    static void testStatic();
};

#endif
2.1.在test.cpp中实现定义的方法


#include "test.h"
#include <iostream>

void Test::test()
{
    printf("Hellow world \n");
    
}
void Test::testStatic()
{
    printf("Hellow world with Static");
}

3.在RootViewController.h文件中定义oc需要调用的方法

-(void)testFunc;

3.1在RootViewController.mm文件中实现上方法,并且和c++文件建立联系

#import "RootViewController.h"
#include "test.h"

static Test* pTest=NULL;
@implementation testObject

-(id)init
{
    if (self=[super init]) {
     
        if (!pTest) {
            pTest=new Test();
        }
    }
    return self;
}
-(void)testFunc
{
    
    if (pTest) {
        pTest->test();  //->  c++ 指针 调用 公有变量和方法
                        //.   c++ 对象  调用 公有变量和方法
    }

    Test::testStatic();
    
}
- (void)dealloc {
  
    if (pTest) {
        delete pTest;
    }
    [super dealloc];
}


@end

4.在viewControler的viewdidLoad方法中实例化一个RootViewController对象并且调用在RootViewController.h声明的方法

   testObject * ttt=[[testObject alloc] init];
    [ttt testFunc];

5.运行查看打印结果

bubuko.com,布布扣



oc中如何调用c++的方法,布布扣,bubuko.com

oc中如何调用c++的方法

标签:c++与oc交互   oc调用c++方法   oc中如何调用c++的方法   c++   oc   

原文地址:http://blog.csdn.net/yinqiangqiang/article/details/37602929

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!