ObjC到C++的转换方法(转)
来源:互联网 发布:淘宝保证金手机怎么退 编辑:程序博客网 时间:2024/05/16 06:58
文章转自:http://www.cocos2d-x.org/projects/cocos2d-x/wiki/Rules_of_translating_objc_to_cpp
Rules of translating objc to cpp
- Rules of translating objc to cpp
- 1. virtual method or not:
- 2. methods of public/protected/private:
- 3. member variables of public/protected/private:
- 4. two-phase construction
- 5. property of objc
- 6. id
1. virtual method or not:
- It's better to declare all cpp member methods without "virtual" keyword;
- But when you're writing a cpp header file, please check if any methods of the parent class are overrided by your current work. Make sure to change them into virtual methods;
- If you inherit a virtual method from parent class, make sure that it inherits the "virtual" keyword.
2. methods of public/protected/private:
- By default, declare all member methods as "public" ;
- If any of the folowing conditions is satisfied, the method must be private;
- this method is declared in .m file;
- this method is in a category named "private";
3. member variables of public/protected/private:
- Declare all member variables as "protected", without any other choice
4. two-phase construction
- HOW TO:
- 1st-phase: set default value for all member variables in the constructor initialization list. But don't do write any logic init in the constructor.
- 2nd-phase: write logic init in a "CMyClass* init(...)" function. If the init failed, return NULL.
- WHY:
- We decided to abandon the usage of try-catch exception mechanism in C++. Do this to reduce the footprint and binary size. As the result, any exception occurring in C++ construction will not be reported to the invoker.
- WHEN:
- two-phase construction isn't force to implement in each class, but just for those classes who have logic step in initialization. In the other words, writing logical initialization in constructor IS FORBIDDEN, especially the situation may return false.
- FOR INVOKERS:
- If the class you will invoke has a "bool init(...)" function, call it immediately after construction.
download sample code: attachment:TwoPhaseConstruction.zip This project is tested in win32 enviroment via VS2008
5. property of objc
6. id
some functions in objc return "id", translate to cpp, we return this "bool" instead. In objc, you can write code just like [[MyClass alloc] init] autorelease]. you don't mind if init failed and return nil, in that case [nil autorelease] will not crash the programe. But in cpp, we return bool to prevent developers write pClass = (new MyClass())->init()->foo(). If init failed and return null, null->fool() will crash and jump out the program in cpp. In the other hand, if the return value of foo() isn't MyClass*, for example, return bool, and the invoker will lost the pointer of "new MyClass" then can't delete it from heap. That's dangerous.
must be translated to
- ObjC到C++的转换方法(转)
- Objc C --Block的浅谈
- 注释转换(C到C++的注释转换)
- ObjC转换为cpp的规则
- [ObjC]objective c判断中英文混合的字符长度的两种方法
- 深入解析 ObjC 中方法的结构
- 深入解析 ObjC 中方法的结构
- 深入解析 ObjC 中方法的结构
- objc.io 2.3 GCD常用方法 (转)
- C#DateTime的转换方法
- 注释转换(从c到c++)
- objc.io 3.1 图像是怎样绘制到屏幕的 (转)
- Objc-C 知识点回顾 二 实例变量 、方法
- objc 类型转换
- objc 类型转换
- 用C++实现十进制到十六进制的两种转换方法
- 大小写转换的方法【C/C++】
- C语言中int到float的强制类型转换(转)
- VMware Fusion中使用迅雷的问题
- JNI(Java Native Interface)调用中考虑的问题
- Statement Execution and Transaction Control【每日一译】--2012-11-04
- Lucene 测试程序
- 学习笔记
- ObjC到C++的转换方法(转)
- iOS loadView,viewDidLoad,viewDidUnload用法
- 找实习之感想
- curl 分析请求头一(部分)
- C++项目配置——VisualStudio开发环境
- linux(_Linux_CentOS) 64位 服务器为Websphere 7 安装及打补丁部署润乾demo
- 2012-11-3 - 11-4 11gR2 "ADMINISTRATOR'S GUIDE" page 585 - 646
- mjpg-streamer项目源码分析
- 面向对象设计原则