ObjC到C++的转换方法(转)

文章转自:http://www.cocos2d-x.org/projects/cocos2d-x/wiki/Rules_of_translating_objc_to_cpp

Rules of translating objc to cpp

1. virtual method or not:

  1. It's better to declare all cpp member methods without "virtual" keyword;
  2. 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;
  3. If you inherit a virtual method from parent class, make sure that it inherits the "virtual" keyword.

2. methods of public/protected/private:

  1. By default, declare all member methods as "public" ;
  2. 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:

  1. Declare all member variables as "protected", without any other choice

4. two-phase construction

  1. 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.
  2. 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.
  3. 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.
  4. FOR INVOKERS:
    • If the class you will invoke has a "bool init(...)" function, call it immediately after construction.
 1#define CCX_BREAK_IF(cond) if(cond) break;
 2#define CCX_SAFE_DELETE(p)  if(p) {delete (p); (p) = NULL;}
 3
 4// declaration
 5class CCar
 6{
 7public:
 8    CCar();
 9    bool    init();
10    virtual ~CCar();
11
12protected:
13    CEngine* m_pEngine;
14    bool     m_bStarted;
15    bool     m_bLocked;
16};
17
18// 1st-phase construction
19// only set the default value & alloc memory
20CCar::CCar()
21:m_pEngine(new CEngine)
22,m_bStarted(false)
23,m_bLocked(true)
24{
25    printf("CCar constructor\n");
26}
27
28// 2st-phase construction
29// do logical initialization
30bool CCar::init()
31{
32    bool bRet = false;
33
34    do
35    {
36        m_bLocked = false;
37
38        CCX_BREAK_IF( !m_pEngine );        // defensive
39        CCX_BREAK_IF( !m_pEngine->start() );    // logic
40
41        // success
42        bRet = true;
43
44    } while(0);
45
46    printf("CCar init\n");
47    return bRet;
48}
49
50// destruction
51CCar::~CCar()
52{
53    if (m_pEngine)
54    {
55        delete m_pEngine;
56        m_pEngine = NULL;
57    }
58
59    printf("CCar destructor\n");
60}
61
62// invoker
63int _tmain(int argc, _TCHAR* argv[])
64{
65    // in heap
66    CCar* pCar = new CCar;
67    if (!pCar->init())
68    {
69        CCX_SAFE_DELETE(pCar);
70    }
71
72    // in stack
73    CCar car;
74    if (!car.init())
75    {
76        // do sth.
77    }
78
79    return 0;
80}

download sample code: attachment:TwoPhaseConstruction.zip This project is tested in win32 enviroment via VS2008

5. property of objc

 1/** CCX_PROPERTY_READONLY is used to declare a protected variable.
 2    We can use get method to read the variable.
 3    @param varType : the type of variable.
 4    @param varName : variable name.
 5    @param funName : "get + funName" is the name of the get method.
 6    @warning : The get method is a public virtual function, you should override it first.
 7            The variables and methods declared after CCX_PROPERTY_READONLY are all public.
 8            If you need protected or private, please declare.
 9*/
10#define CCX_PROPERTY_READONLY(varType, varName, funName)\
11    protected: varType varName;\
12    public: virtual varType get##funName(void);
13
14/** CCX_PROPERTY is used to declare a protected variable.
15    We can use get method to read the variable, and use the set method to change the variable.
16    @param varType : the type of variable.
17    @param varName : variable name.
18    @param funName : "get + funName" is the name of the get method.
19                     "set + funName" is the name of the set method.
20    @warning : The get and set methods are public virtual functions, you should override them first.
21            The variables and methods declared after CCX_PROPERTY are all public.
22            If you need protected or private, please declare.
23*/
24#define CCX_PROPERTY(varType, varName, funName)\
25    protected: varType varName;\
26    public: virtual varType get##funName(void);\
27    public: virtual void set##funName(varType var);
28
29/** CCX_SYNTHESIZE_READONLY is used to declare a protected variable.
30    We can use get method to read the variable.
31    @param varType : the type of variable.
32    @param varName : variable name.
33    @param funName : "get + funName" is the name of the get method.
34    @warning : The get method is a public inline function.
35            The variables and methods declared after CCX_SYNTHESIZE_READONLY are all public.
36            If you need protected or private, please declare.
37*/
38#define CCX_SYNTHESIZE_READONLY(varType, varName, funName)\
39    protected: varType varName;\
40    public: inline varType get##funName(void){ return varName; }
41
42/** CCX_SYNTHESIZE is used to declare a protected variable.
43    We can use get method to read the variable, and use the set method to change the variable.
44    @param varType : the type of variable.
45    @param varName : variable name.
46    @param funName : "get + funName" is the name of the get method.
47                     "set + funName" is the name of the set method.
48    @warning : The get and set methods are public  inline functions.
49            The variables and methods declared after CCX_SYNTHESIZE are all public.
50            If you need protected or private, please declare.
51*/
52#define CCX_SYNTHESIZE(varType, varName, funName)\
53    protected: varType varName;\
54    public: inline varType get##funName(void){ return varName; }\
55    public: inline void set##funName(varType var){ varName = var; }

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.

@interface CTest
-(id) foo();

must be translated to 
class CTest
{
    bool foo();
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值