推荐到Delphi社区看看 http://bdn.borland.com/article/0,1410,27603,00.htmlHere's a quick guide to implementing C++-like templates in Object Pascal. By Rossen Assenov.
下面是DeCal的介绍: The Standard Delphi Library (SDL) by Soletta is a powerful library of reusable container classes, generic algorithms, and an easy to use persistence mechanism. SDL is designed for intermediate to advanced Delphi programmers who have a need for sophisticated data structures or who wish to take advantage of SDL’s large library of generic algorithms. SDL is also highly appropriate for programmers who are experienced with the C++ STL (Standard Template Library), or ObjectSpace’s JGL (Java Generic Library). SDL is an adaptation of Stepanov and Lee’s concepts to the Delphi environment.
据李维说,delphi已经提出了模板方案,因为C#将要支持泛型了
TmyClassClass=class of TmyClass; 和 myString=type of string 是完全不同的两个概念,前者是类引用类型,后者是类的别名。————————————————————————————————— 宠辱不惊,看庭前花开花落,去留无意;毁誉由人,望天上云卷云舒,聚散任风。 —————————————————————————————————
除非更改编译器,我门就祈祷borland 吧
TOOLS ---> Editor Properties ---> code Insight ----> Code Templetes
可以建立一些长用的函数,以后CTRL+J就可以使用自定义的代码段了。
TmyClass=class
end;
TmyClass1=class(TmyClass)
end;
TmyClass2=class(TmyClass)
end;
TmyClassClass=class of TmyClass;
myString=type of string;//强制类型,用得很少,记得不太清楚是不是这样声明了function CreatemyClass(ClassTYpe:TmyClassClass):TmyClass;
begin
Result:=ClassType.Create;
end;
你就可以这样创建类的实例了:
CreatemyClass(TmyClass1);
CreatemyClass(TmyClass2);
都可以通过变通的方法实现
如果从OOP的角度说的话
反而容易使初学者迷惑
function Add(const X);
那么你说 procedure foo(args:array of const)也算不算呢?模板实际上是在编译时编译器给单独类型创建了单独的方法或者类等,这样就可以减少多态(或者.net中的装箱操作)而带来的效率影响,以提高程序运行效率. 个人理解,我对C++不是很熟,只能构理解到这个程度
2.宏又是什么 个人理解宏是用来替换的,在编译时进行代码替换。例如 #define add(x,y) ((x)+(y)) ,那么你可以像用函数一样来用add了,不过编译器真正编译的代码绝对不会是add(x,y),都给你替换成((x)+(y))了。相应的,宏也往往代替const来定义常量。
由于经常听到有人把Delphi跟C++比,知道了一些优劣,有兴趣了解一下object pascal,
看到这里讨论一些c++相关的技术,想在此交个朋友,共同进步。至于模板技术,我不过是一个新手,看下面代码:
下面是一个开平方的代码,虽然没有什么实际作用,但对描述这种技术有帮助。template <int N, int LO=0, int HI=N>
struct CompileTimeSqrt {
enum { mid = (LO+HI+1)/2 };
enum { result = (N<mid*mid) ? CompileTimeSqrt<N,LO,mid-1>::result
: CompileTimeSqrt<N,mid,HI>::result };
};template <int N, int M>
struct CompileTimeSqrt<N,M,M> {
enum { result = M };
};模板不象是宏,因为宏是对编译器透明的,而模板不是,
宏不包含类型信息,
模板可以说是编译期多态。
如果想了解跟多的话可以参考stl的实作。有时觉得泛型技术虽然是很有发展的技术,但对程序员要求太高。
不知道delphi的朋友们如何看待以后的发展。
学习ing......
Delphi和C++ Builder是兄弟开发工具,
Delphi的效率会否高于C++ Builder。因为我听说Delphi内置string型别,那么很可能在大量操作string的场合下,Delphi的效率会明显优于C++ Builder,对不对?
我知道stl的basic_string模板大部分实作品都是实现延迟拷贝,Delphi的string是否也实现延迟拷贝?
他们之间的效率相差多大呢?
请指教......
可能我对泛型的概念的认识有偏差,
tobject和vc++ mfc 的CObject有什么区别?
delphi里是如何使用tobject实现c++ template的。
虽然他牺牲了这些功能,但是那些大部分是很少用到而且易出错的功能。
而且这是编译速度加快,大家应该都有深刻体会吧
建议有一些人应该回去看看什么是C++STL模板,再来讨论;
http://bdn.borland.com/article/0,1410,27603,00.htmlHere's a quick guide to implementing C++-like templates in Object Pascal. By Rossen Assenov.
The Standard Delphi Library (SDL) by Soletta is a powerful library of reusable container
classes, generic algorithms, and an easy to use persistence mechanism. SDL is designed for
intermediate to advanced Delphi programmers who have a need for sophisticated data
structures or who wish to take advantage of SDL’s large library of generic algorithms. SDL
is also highly appropriate for programmers who are experienced with the C++ STL
(Standard Template Library), or ObjectSpace’s JGL (Java Generic Library). SDL is an
adaptation of Stepanov and Lee’s concepts to the Delphi environment.
和
myString=type of string
是完全不同的两个概念,前者是类引用类型,后者是类的别名。—————————————————————————————————
宠辱不惊,看庭前花开花落,去留无意;毁誉由人,望天上云卷云舒,聚散任风。
—————————————————————————————————