StevenHuSir

//Person * p = [[Person alloc] init];

//alloc :分配内存空间  init:初始化对象,属性&方法

//苹果不建议我们使用Objc_msgSend , 在build-setting 搜索 msg 打开

//导入 #import <objc/message.h>

// objc_msgSend        消息发送

//  objc_getClass        获取类名

//  sel_registerName  注册方法编号

//OC 转C  OC 中 1.SEL 方法编号

   Person * p = objc_msgSend(objc_getClass("Person"), sel_registerName("alloc"));

 

//p = [p init];

    p = objc_msgSend(p, sel_registerName("init"));

 

//[p eatWith:@"汉堡!!"];

//传参数

 

    objc_msgSend(p, sel_registerName("eatWith:"),@"汉堡!!");

//给父类发送消息

 

         HKPerson * p = [[HKPerson alloc] init];

    //objc_super 结构体指针

    //class_getSuperclass 获取父类类型

    //定义hkSuper 结构体

    struct objc_super hkSuper = {p ,class_getSuperclass(objc_getClass("HKPerson"))};

    //给父类发送消息

    objc_msgSendSuper(&hkSuper, @selector(eatWith:),@"汉堡");

 

clang编译工具将OC main.m 编译成C++代码

 

$ clang -rewrite-objc main.m 

将OC 转 C++

Person * p = [Person alloc];
p = [p init];
int main(int argc, const char * argv[]) {
    /* @autoreleasepool */ { __AtAutoreleasePool __autoreleasepool; 
        Person * p = ((Person *(*)(id, SEL))(void *)objc_msgSend)((id)objc_getClass("Person"), sel_registerName("alloc"));
        p = ((Person *(*)(id, SEL))(void *)objc_msgSend)((id)p, sel_registerName("init"));
    }
    return 0;
}

 

 

 

 

 

相关文章:

  • 2022-01-18
  • 2021-08-24
  • 2021-06-28
  • 2022-01-25
  • 2021-06-08
  • 2022-12-23
  • 2021-10-23
  • 2021-12-07
猜你喜欢
  • 2021-08-07
  • 2021-10-15
  • 2022-01-09
  • 2021-07-25
  • 2021-05-20
  • 2022-12-23
相关资源
相似解决方案