안녕하세요. (주)나이렉스 인터내셔널의 실장 서현철입니다.
저는 원래는 C,C++로 프로그램을 짜다가 몇년전에 델파이를 시작했습니다.
그런데, C에서 잘 쓰던 함수 포인터에 대한 미련이 남아서 델파이에서 혹시
똑같은 방법이 있지 않을까? 연구해봤습니다. 그래서, 결국 해냈습니다.
저와 같은 경우를 당한 분들께 이 내용을 바칩니다.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
// 형을 선언한다.
tPtrFunc = record
Name : String; // 함수를 나타내는 문자열
Func : Procedure; // 인수가 없는 함수 선언
fun2 : function (a : Integer) : Integer; // 반환값이 있는 함수 선언
end;
TForm1 = class(TForm)
Button1: TButton; // --+
Button2: TButton; // :--- 콤포넌트들
Button3: TButton; // :
Button4: TButton; // --+
procedure FormCreate(Sender: TObject); // 초기화를 위한 함수
procedure Button1Click(Sender: TObject); // --+
procedure Button3Click(Sender: TObject); // :--- 예제를 보기 위한 처리 루틴
procedure Button2Click(Sender: TObject); // :
procedure Button4Click(Sender: TObject); // --+
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
aPtrFunc : array[0..5] of tPtrFunc; // 선언된 형의 배열 선언
implementation
{$R *.DFM}
function aa(a : Integer) : Integer; // 반환값이 있는 함수
var
i : Integer;
j : Integer;
begin
i := 1;
j := 4;
Result := i + j + a;
end;
// 예제를 위한 함수(배열에 초기화 하기 위한 함수.
procedure a;
begin
ShowMessage(aPtrFunc[0].Name + ' : a' + IntToStr(aPtrFunc[0].fun2(3)));
end;
// 예제를 위한 함수(배열에 초기화 하기 위한 함수.
procedure b;
begin
ShowMessage(aPtrFunc[1].Name + ' : b');
end;
// 예제를 위한 함수(배열에 초기화 하기 위한 함수.
procedure c;
begin
ShowMessage(aPtrFunc[2].Name + ' : d');
end;
// 예제를 위한 함수(배열에 초기화 하기 위한 함수.
procedure d;
begin
ShowMessage(aPtrFunc[3].Name + ' : e');
end;
// 배열의 초기화
procedure TForm1.FormCreate(Sender: TObject);
begin
aPtrFunc[0].Name := '첫번째 자료 처리';
aPtrFunc[0].Func := a;
aPtrFunc[0].fun2 := aa;
aPtrFunc[1].Name := '두번째 자료 처리';
aPtrFunc[1].Func := b;
aPtrFunc[2].Name := '세번째 자료 처리';
aPtrFunc[2].Func := c;
aPtrFunc[3].Name := '네번째 자료 처리';
aPtrFunc[3].Func := d;
end;
// 배열에 들어가 있는 함수의 실행
procedure TForm1.Button1Click(Sender: TObject);
begin
aPtrFunc[0].Func;
end;
// 배열에 들어가 있는 함수의 실행
procedure TForm1.Button3Click(Sender: TObject);
begin
aPtrFunc[1].Func;
end;
// 배열에 들어가 있는 함수의 실행
procedure TForm1.Button2Click(Sender: TObject);
begin
aPtrFunc[2].Func;
end;
// 배열에 들어가 있는 함수의 실행
procedure TForm1.Button4Click(Sender: TObject);
begin
aPtrFunc[3].Func;
end;
end.
조그이나마 도움이 되었으면 합니다.
|