放假时学了点delphi,觉得delphi比c++严谨,不过灵活性稍差点,总结下有这么几个方面:
1:)没有模板,C++的模板确实有很好的重用性;大家是每个数据类型都准备一套算法吗?
2:)容器没有链表;是有现成的链表控件吗?还设有用TList将就一下?
3:)容器没有键值,听说Delphi中有类似的实现但我不清楚,希望它有:);
4:)类没有类似C++的静态成员变量,只有静态成员方法;
不知道大家在遇到这些问题是怎么解决的?

解决方案 »

  1.   

    1.Delphi2009以前,的确木有泛型支持。即木有模板,这个有变通方法,比如,使用指针等等。
       Delphi2010开始,Delphi支持泛型,但是这种支持相对于C++的模板还是有不少限制。
       关键原因是,class不支持运算符号重载,因此对于一个 T a,b; 不能写出这样的代码 a+b。
       所以,并不是很容易操作。2.这个就不说了,这个随便自己处理一个就可以,太简单了。
      在Delphi2009之后的版本有 TList<T> 这种内置的支持泛型的容器3.容器没有键值,估计你说的是map,Delphi2009之前的版本有替代品THashedStringList,但有一些局限性。Delphi2009之后,有容器TDictionary<Key, Value>4.Delphi2009之前版本(我不是太确定),没有类静态成员变量,一般用Unit的局部变量代替,即implemention部分定义的单元内全局变量。Delphi2009之后的版本有类静态成员。
    另外,Delphi的类成员方法不能理解为C++中的静态成员方法,他们是不一样的。
    Delphi2009之后的版本中,通过class procedure XXX; static; 方式可以实现C++中的静态成员方法
      

  2.   


    class不支持运算符号重载”应该是不正确的,见:http://edn.embarcadero.com/article/34324TMyClass = class
        class operator Add(a, b: TMyClass): TMyClass; // Addition of two operands of type TMyClass
        class operator Subtract(a, b: TMyClass): TMyclass; // Subtraction of type TMyClass
        class operator Implicit(a: Integer): TMyClass; // Implicit conversion of an Integer to type TMyClass
        class operator Implicit(a: TMyClass): Integer; // Implicit conversion of TMyClass to Integer
        class operator Explicit(a: Double): TMyClass; // Explicit conversion of a Double to TMyClass
    end;// Example implementation of Add class operator 
    TMyClass.Add(a, b: TMyClass): TMyClass;
    begin
      ...
    end;var
    x, y: TMyClassbegin
      x := 12; // Implicit conversion from an Integer 
      y := x + x; // Calls TMyClass.Add(a, b: TMyClass): TMyClass 
      b := b + 100; // Calls TMyClass.Add(b, TMyClass.Implicit(100)) 
    end;
      

  3.   


    我记得,好像是中间有个For .net的东西,其中的Class是支持Operator Overloading 
    但是原生的Win32/Win64的Delphi的Class的确是不支持Operator Overloading 的
    不过Record是支持的。
    Delphi2010 DelphiXE DelphiXE2中的帮助
      

  4.   

    Delphi2010 DelphiXE DelphiXE2中的帮助:
    This topic describes Delphi's operator methods and how to overload them. Contents
    1 About Operator Overloading 
    2 Declaring Operator Overloads 
    3 See Also 
    4 Code Samples 
     
    [edit] About Operator Overloading 
    Delphi for Win32 allows certain functions, or "operators", to be overloaded within record declarations. The name of the operator function maps to a symbolic representation in source code. For example, the Add operator maps to the + symbol. The compiler generates a call to the appropriate overload, matching the context (that is, the return type, and type of parameters used in the call), to the signature of the operator function. 
      

  5.   

    1:)没有模板,C++的模板确实有很好的重用性;大家是每个数据类型都准备一套算法吗?
    Delphi2009,2010,DelphiXE,DelphiXE2是有模板的
    2:)容器没有链表;是有现成的链表控件吗?还设有用TList将就一下?
    在算法中自从有了动态数组以后链表用得少了很多.虽然不能完全取代.一般需要链表的话就自己写一个
    3:)容器没有键值,听说Delphi中有类似的实现但我不清楚,希望它有:);
    同1.Delphi2009,2010,XE,XE的容器类中的字典就是C++STL中的map.某些方面还好用一些
    4:)类没有类似C++的静态成员变量,只有静态成员方法;
    同1.Delphi2009以后是有静态变量的.
    另外早期Delphi的class方法也不等同于C++的静态方法.class方法中可以用self关键字,这个self是类,不是实例.C++的静态方法没这个.后期Delphi加了static关键字以后才有对应C++的静态方法.
    还有早期Delphi虽然没有类的静态变量,不过也不重要.完全可以用全局变量来模拟.静态变量经过编译器编译后和全局变量是等同的.
    DelphiXE以后有一个新的功能就是类的构造析构函数.就是类本身在创建的时候的构造器和析构方法.对于静态变量的初始化还是很方便的.
      

  6.   

    受益匪浅:)
    又想起2个-
    5:)位域!这个在编写工业控制接口时比较方便,但是在delphi中我没发现,目前我对Delphi定位只能是桌面应用,不能用于工业控制。
    6:)怎么在Delphi中使用MySQL呢?翻了好几本书,例子总是SQL Server,就是不知道如何使用MySQL,气氛啊
    另外目前还是学习中,用的是D7,看好多大侠用的是2009及以后的版本,口水中,目前还没有更换的想法,先用D7学好吧。
      

  7.   

    5。单个bit,其实用Delphi的集合更舒服,直观。
    但是,如果说有,int a:3; 这样的。Delphi中没有对应的。只能自己用位操作特别处理6。MySQL连接方式有很多。ado odbc,DBExpress等等,我推荐ZeosDBO,开源的
       不过很长时间没维护了,好在代码都用,不够用自己还可以改
      

  8.   

    不好意思,我没有相同怎样用集合实现位域的模拟;MySQL我安装了它的ODBC,但是不会设置Delphi中的控件实现,是那个udl文件不知道如何设置,汗!
      

  9.   


    关于第4点,类是有静态成员变量的,通过class var来声明(至少在Delphi2007之后应该是可以的)
      

  10.   

    D7下,类似C++ map的带Key容器,我一般用TStringList;