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.
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 ”);
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 ”);
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 ”);
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();
}