Delphi 2006引入了新的记录功能,使其更加"面向对象".
在哪种情况下,记录类型更适合于设计而不是类类型?使用这些记录类型有哪些优势?
是否有可能(在Delphi中)重载类中的运算符.我前段时间读过这篇文章只能用于记录,但是我发现的信息类似于以下代码中的类:
type
TMyClass = class
class operator Implicit(a: Integer): TMyClass;
end;
class operator TMyClass.Implicit(a: Integer): TMyClass;
begin
// ...
end;
Run Code Online (Sandbox Code Playgroud)
它是(修改)从地址:http: //docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/devcommon/operatoroverloads_xml.html
但是当我尝试使用它时(在Delphi XE中)我得到:
预期的程序,功能,财产或VAR(E2123)
我想为矩阵操作创建我自己的简单类,并且在类中使用重载操作符的可能性是非常期待的机会.
Regars,Artik
首先,我需要知道,如果我想做的事情是可能的.如果有可能,我需要知道如何.
它更容易证明问题而不是解释它,所以这里:
我有一个"增强记录"(目的 - 虽然对这个问题不重要 - 是产生一个"智能字符串"类型,以取代普通的字符串类型):
TLKString = record
Value: String;
// Some methods here to operate on and build String values
// Allows me to assign String values directly to "instances"
// of this record type! I have others (hence "overload") to
// handle other data types (such as Integer etc.)
class operator Implicit(const AValue: String): TLKString; overload;
end;
Run Code Online (Sandbox Code Playgroud)
我现在可以使用这个TLKString类型,如下所示:
var
LSmartString: TLKString;
begin
LSmartString := 'Hello World'; // The "Implicit" operator then
// assigns this to LSmartString.Value …Run Code Online (Sandbox Code Playgroud)