현재 델파이를 구현할 수있는 방법이 있습니까?Delphi Win32의 클래스 도우미 및 문자열
가) 문자열 (연산자 오버로드 (즉,와 클래스)로. +, =)
b)는 클래스 도우미의 그래서 하나 추가 할 수 있습니다 사용자 정의 문자열 방법
나는 문자열이 너무 기본 유형입니다 수집 클래스 헬퍼는 클래스를 설정하지 않으면 작동하지 않습니다.
현재 델파이를 구현할 수있는 방법이 있습니까?Delphi Win32의 클래스 도우미 및 문자열
가) 문자열 (연산자 오버로드 (즉,와 클래스)로. +, =)
b)는 클래스 도우미의 그래서 하나 추가 할 수 있습니다 사용자 정의 문자열 방법
나는 문자열이 너무 기본 유형입니다 수집 클래스 헬퍼는 클래스를 설정하지 않으면 작동하지 않습니다.
사용자 정의 함수/프로 시저를 작성하는 더 좋은 해결책이 아닙니까? 예를
Function StrToBase64(AString): string;
Procedure StrToGridLayout(AString: string; AGrid: TDBGrid);
Function ExtractWord(aString): string;
Function GetStrColumn(aString: string; aCol: integer): string;
를 들어
그리고 당신은 그룹에 이러한 기능의 기능 범주에서 같은 유닛에 거주/절차 당신은 이것에 대한 기록을 사용할 수 원하는 경우 : 다음
TStringConversions = record
class Function StrToBase64(AString): string;
class Procedure StrToGridLayout(AString: string; AGrid: TDBGrid);
end;
TStringParsing = record
class Function ExtractWord(aString): string;
class Function GetStrColumn(aString: string; aCol: integer): string;
end;
을 그리고을, 당신을 훨씬 더 깨끗한 방식으로 코드에서 호출 할 수 있습니다.
myFirstWord := TStringParsing.ExtractWord('Delphi is a very good tool');
HTH
예, string은 특수 컴파일러가 추가 된 기본 유형입니다.
내가 원하는 연산자 오버로딩을 알지 못합니다. +와 =는 이미 연결 연산자와 항등 연산자로 작동합니다.
그러나 나 자신과 비슷한 것을하려고 생각했습니다. 암시 적 변환기와 오버로드 된 add 및 equals 연산자가있는 레코드 유형에서 작동 할 수 있습니다 (Win32 Delphi에서만 레코드에 연산자 오버로드가있을 수 있습니다.) D2006 (2005)에서만 사용 가능합니다.
일부 성능이있을 수 있습니다. 맞았다.
구문은 다음과 같을 것이다
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
TString = record
private
Value : string;
public
class operator Add(a, b: TString): TString;
class operator Implicit(a: Integer): TString;
class operator Implicit(const s: string): TString;
class operator Implicit(ts: TString): String;
function IndexOf(const SubStr : string) : Integer;
end;
var
Form1: TForm1;
implementation
class operator TString.Add(a, b : TString) : TString;
begin
Result.Value := a.Value + b.Value;
end;
class operator TString.Implicit(a: Integer): TString;
begin
Result.Value := IntToStr(a);
end;
class operator TString.Implicit(ts: TString): String;
begin
Result := ts.Value;
end;
function TString.IndexOf(const SubStr : string) : Integer;
begin
Result := Pos(SubStr, Value);
end;
class operator TString.Implicit(const s: string): TString;
begin
Result.Value := s;
end;
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
ts : TString;
begin
ts := '1234';
ShowMessage(ts);
ShowMessage(IntToStr(Ts.IndexOf('2')));
end;
end.
은 분명히 당신은뿐만 아니라 "레코드 도우미"를 가질 수 있지만 내가 직접 한 번도 안 써봤.
레코드 도우미도 만들 수 있습니다. –
OU가 새로운 것을 매일 배웁니다 :) –
다른 레코드에 할당을 통해 레코드를 복사하면 문자열 참조 카운팅 문제가 발생하지 않습니까? –
Delphi에서 연산자 오버로딩 (Delphi 2006 이후)은 문자열이 아닌 기본 제공 유형이 아닌 클래스에있는 레코드에만 사용할 수 있습니다.
델파이는 가비지 콜렉션이 없기 때문에 연산자 오버로딩은 값 유형 (힙에 살지 않는 유형)으로 제한됩니다.
CodeRage III Replay download page에서 내 세션 "레코드, 메서드 및 연산자 오버로드가있는 Null 가능 형식"의 재생을 다운로드 할 수 있습니다. 세션 이름을 검색하기 만하면됩니다.
page with the download for the session samples and slides도 있습니다.
여기에는 Delphi 2007 이상에서 해결 된 Delphi 2006 컴파일러의 몇 가지 문제에 대한 설명이 포함되어 있습니다.특히 델파이의 Can I overload operators for my own classes in Delphi?
버전 :
또한이 질문을 참조하십시오? –