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

// declaration

5class CCar

6{

public:

8 CCar();

9 bool init();

10 virtual ~CCar();

11

protected:

13 CEngine* m_pEngine;

14 bool m_bStarted;

15 bool m_bLocked;

16};

17

// 1st-phase construction

// only set the default value & alloc memory

20CCar::CCar()

21:m_pEngine(new CEngine)

22,m_bStarted(false)

23,m_bLocked(true)

24{

printf(“CCar constructor\n”);

26}

27

// 2st-phase construction

// 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 } );

45

46 printf(“CCar init\n”);

47 return bRet;

48}

49

// destruction

51CCar::~CCar()

52{

53 if (m_pEngine)

54 {

55 delete m_pEngine;

56 m_pEngine = NULL;

57 }

58

printf(“CCar destructor\n”);

60}

61

// invoker

63int _tmain(int argc, _TCHAR* argv[])

64{

// in heap

66 CCar* pCar = new CCar;

67 if (!pCar->init())

68 {

69 CCX_SAFE_DELETE(pCar);

70 }

71

// in stack

73 CCar car;

74 if (!car.init())

75 {

76 // do sth.

77 }

78

79 ;

80}

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

5. property of objc

/** CCX_PROPERTY_READONLY is used to declare a protected variable.

We can use get method to read the variable.

@param varType : the type of variable.

@param varName : variable name.

@param funName : “get + funName” is the name of the get method.

@warning : The get method is a public virtual function, you should override it first.

The variables and methods declared after CCX_PROPERTY_READONLY are all public.

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

/** CCX_PROPERTY is used to declare a protected variable.

We can use get method to read the variable, and use the set method to change the variable.

@param varType : the type of variable.

@param varName : variable name.

@param funName : “get + funName” is the name of the get method.

“set + funName” is the name of the set method.

@warning : The get and set methods are public virtual functions, you should override them first.

The variables and methods declared after CCX_PROPERTY are all public.

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

/** CCX_SYNTHESIZE_READONLY is used to declare a protected variable.

We can use get method to read the variable.

@param varType : the type of variable.

@param varName : variable name.

@param funName : “get + funName” is the name of the get method.

@warning : The get method is a public inline function.

The variables and methods declared after CCX_SYNTHESIZE_READONLY are all public.

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

/** CCX_SYNTHESIZE is used to declare a protected variable.

We can use get method to read the variable, and use the set method to change the variable.

@param varType : the type of variable.

@param varName : variable name.

@param funName : “get + funName” is the name of the get method.

“set + funName” is the name of the set method.

@warning : The get and set methods are public inline functions.

The variables and methods declared after CCX_SYNTHESIZE are all public.

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();

}

Moving From Objective-C to C++的更多相关文章

  1. Automake

    Automake是用来根据Makefile.am生成Makefile.in的工具 标准Makefile目标 'make all' Build programs, libraries, document ...

  2. Artistic Style 3.1 A Free, Fast, and Small Automatic Formatter for C, C++, C++/CLI, Objective‑C, C#, and Java Source Code

    Artistic Style - Index http://astyle.sourceforge.net/ Artistic Style 3.1 A Free, Fast, and Small Aut ...

  3. Moving Computation is Cheaper than Moving Data

    https://hadoop.apache.org/docs/r1.2.1/hdfs_design.html Introduction The Hadoop Distributed File Syst ...

  4. [LeetCode] Moving Average from Data Stream 从数据流中移动平均值

    Given a stream of integers and a window size, calculate the moving average of all integers in the sl ...

  5. Moving Average from Data Stream

    Given a stream of integers and a window size, calculate the moving average of all integers in the sl ...

  6. 卡通图像变形算法(Moving Least Squares)附源码

    本文介绍一种利用移动最小二乘法来实现图像变形的方法,该方法由用户指定图像中的控制点,并通过拖拽控制点来驱动图像变形.假设p为原图像中控制点的位置,q为拖拽后控制点的位置,我们利用移动最小二乘法来为原图 ...

  7. Objective C中的ARC的修饰符的使用---- 学习笔记九

    #import <Foundation/Foundation.h> @interface Test : NSObject /** * 默认的就是__strong,这里只是做示范,实际使用时 ...

  8. Objective的字符串拼接 似乎没有Swift方便,但也可以制做一些较为方便的写法

    NSString *str1 = @"字符串1"; NSString *str2 = @"字符串2"; //在同样条件下,Objective的字符串拼接 往往只 ...

  9. LeetCode Moving Average from Data Stream

    原题链接在这里:https://leetcode.com/problems/moving-average-from-data-stream/ 题目: Given a stream of integer ...

  10. [转] 从 C 到 Objective C 入门1

    转自: http://blog.liuhongwei.cn/iphone/objective-c/ 进军iPhone开发,最大的难点之一就是怪异的Objective C语法了.不过,了解之后才发现,原 ...

随机推荐

  1. CSS3 Transitions, Transforms和Animation使用简介与应用展示

    CSS3 Transitions, Transforms和Animation使用简介与应用展示 by zhangxinxu from http://www.zhangxinxu.com本文地址:htt ...

  2. unix network programming(3rd)Vol.1 [第1章]《读书笔记系列》

    文章最开头介绍了 获取时间的C/S 模型的代码, 还用了实现了IPV6的版本 unix 介绍了errno值,以及在多进程/多线程中的问题 多线程中不用全局errno,而是用返回值 处理error 详细 ...

  3. MAC虚拟机NAT方式共享上网设置

    有部分FY需要,我写一下我的方法吧,当初安装完MAC后,无法上网,网络搜索用的是HOST-only方法,试了几次都没有成功,后来尝试NAT方法,发现很简单. 我的主机系统:win7 64位,自动获取I ...

  4. [selenium webdriver Java]常用api

    1. 获取元素文本 WebElement类的getText()方法返回元素的innerText属性.所以元素里如果有子节点一样也会被返回出来.如下所示 public class GetText { @ ...

  5. codeforces 617B Chocolate

    题意: 在给定01串中,问能分割成多少个子串?每个子串只有一个1. dp #include<iostream> #include<string> #include<alg ...

  6. 字符串旋转(str.find()---KMP)

    此题旋转带有技巧性,问题转化为常见的问题,熟练STL可以直接用str.find()函数,其是主要想用KMP算法实现字符串的查找算法... //如果对于一个字符串A,将A的前面任意一部分挪到后边去形成的 ...

  7. flappy pig小游戏源码分析(2)——解剖option

    今天继续分析flappy bird的源码.重温一下源码的目录结构. 在本系列第一篇中我们分析了game.js文件,也就是整个程序的架构.这一篇我们来看看option.js文件,这个文件的内容很简单,主 ...

  8. 《Genesis-3D开源游戏引擎完整实例教程-跑酷游戏篇04:如何实现触控操作》

    4.如何实现触控操作 触控操作概述: 随着APPLE.Samsung.SONY等众多公司,将掌机.电脑和手机等产品在触控领域的不断探索,以触控为操作的机型越来越多的被投放到市场当中.触控游戏.触控软件 ...

  9. Java注解处理器使用详解

    在这篇文章中,我将阐述怎样写一个注解处理器(Annotation Processor).在这篇教程中,首先,我将向您解释什么是注解器,你可以利用这个强大的工具做什么以及不能做什么:然后,我将一步一步实 ...

  10. ABAP开发基础知识:内表(Internal Table)

    http://www.cnblogs.com/foxting/archive/2012/03/19/2406830.html 内表与结构体基本类似,它同样是程序运行中被临时创建的一个存储空间,它是一个 ...