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

*)

 


你可能感兴趣的:(【Delphi】)