C++与DELPHI转换规则

C++与DELPHI转换规则

一个老外的PAS单元里的描述,感觉不错贴了上来,应该是比较容易理解的.

(* Translation notes :
  -------------------

  - Delphi classes are pointers :
  ----------- Delphi --------------+---------- C -------------------------------
  var                              | {
   MyInstance:TMyClass;            |   TMyClass *MyInstance;
  begin                            |
   MyInstance:=TMyClass.Create;    |   MyInstance=new TMyClass;
  ------------------------------------------+-----------------------------------
  - Delphi use a "." for classes fields, not "->"

  - Static classes aren't available under Delphi.
  ----------- C -------------------+---------- Delphi --------------------------
  {                                |
   TMyClass MyClasse;              |  need the same code as above !
                                   |
                                   |
  ---------------------------------+--------------------------------------------

  - Function returned value is called "Result", it can be read and write

  - There's no "return" keyword, use "Result:=Value; exit;"

  - C "FOR", look like a While for Delphi, Delphi "FOR" have no exit condition.
  -----------C---------------------------+--------Delphi------------------------
   for (i=0; i<10; i++) {                | for i:=0 to 9 do begin
   }                                     | end;
                                         |
   for (i=0; (i<10)&&(s); i++, j++) { | i:=0; while (i<10)and(s<>0) do begin
   }                                     | inc(i); inc(j); end;
                                         |
   for(; childCount > 0; childCount--) { | while ChildCount>0 do begin
   }                                     | dec(ChildCount); end;
  ------------------------------------------------------------------------------

  - null pointer is "nil"; pointer isn't integer, can't assign 0 to a pointer !

  - float => single

  - Delphi "Property" can replace get/set method :
  -----------C---------------------------+--------Delphi------------------------
   class MyClass {                       | MyClass=class
    private:                             |  private
     int m_Field;                        |   m_Field:integer;
    public:                              |  public
     int getField();                     |   property Field:integer read m_Field write m_Field;
     void setField(int value);           | end;
   }                                     | //you can also use get/set method !
                                         | //property Field:integer read getField write setField;
  ------------------------------------------------------------------------------
  - I've replaced :
   * vector class by Delphi dynamic arrays : reserve -> SetLength()
   * list by TList
   * map by dynamic arrays of record...
  ------------------------------------------------------------------------------

  Fix #1, Delphi need an Assign() method to copy class fields
  -----------C---------------------------+--------Delphi------------------------
   CalVector x(1,2,3);                   | x:=CalVector.Create(1,2,3);
   Calvector y=x;                        | y:=CalVector.Create(0,0,0);
                                         | y.Assign(x);

*)

 


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值