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:

  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();1112protected:13    CEngine* m_pEngine;14    bool     m_bStarted;15    bool     m_bLocked;16};1718// 1st-phase construction19// only set the default value & alloc memory20CCar::CCar()21:m_pEngine(new CEngine)22,m_bStarted(false)23,m_bLocked(true)24{25    printf("CCar constructor\n");26}2728// 2st-phase construction29// do logical initialization30bool CCar::init()31{32    bool bRet = false;3334    do35    {36        m_bLocked = false;3738        CCX_BREAK_IF( !m_pEngine );        // defensive39        CCX_BREAK_IF( !m_pEngine->start() );    // logic4041        // success42        bRet = true;4344    } while(0);4546    printf("CCar init\n");47    return bRet;48}4950// destruction51CCar::~CCar()52{53    if (m_pEngine)54    {55        delete m_pEngine;56        m_pEngine = NULL;57    }5859    printf("CCar destructor\n");60}6162// invoker63int _tmain(int argc, _TCHAR* argv[])64{65    // in heap66    CCar* pCar = new CCar;67    if (!pCar->init())68    {69        CCX_SAFE_DELETE(pCar);70    }7172    // in stack73    CCar car;74    if (!car.init())75    {76        // do sth.77    }7879    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);1314/** 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);2829/** 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; }4142/** 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();}

原创粉丝点击