能不能将一个类型作为参数传递到函数里面?(100分)

S

screen

Unregistered / Unconfirmed
GUEST, unregistred user!
比如:myList : TList;
function aaa(myList : TList
XXX) : integer
其中XXX我不知道怎么表示,就是说,XXX就是TList这种类型,
我在函数里面就可以 myList := XXX.create;
 
function aaa(var myList : TList
XXX) : integer
 
现在人,很奇怪,基本的东西不学不用,总爱用这些不着边际的东东.
可以用类类型来试一试,看成不成?
所有类的总体也是一个类,它就是类类型.
 
type
TListClass = class of TList;

function aaa(var myList: TList
XXX: TListClass): Integer;
begin
myList := XXX.Create;
result := 0;
end;
 
你这种方式是可行的比如:利用类引用,下面的声明在我记不得是delphi什么单元已经声明
过:
TControlClass:class of Tcontrol
function aaa(var myList : TList
XXX:TcontrolClass) : integer
这样你就可以把TCheckBox等可视控件传给该函数如这样调用
aaa(mylist,TCheckBox)
aaa(mylist,TCombobox)
这不是不着边际的东西,这是动态创建控件的东西。
 
給你一個例子

procedure TApplication.CreateForm(InstanceClass: TComponentClass
var Reference);
var
Instance: TComponent;
begin
Instance := TComponent(InstanceClass.NewInstance);
TComponent(Reference) := Instance;
try
Instance.Create(Self);
except
TComponent(Reference) := nil;
raise;
end;
if (FMainForm = nil) and (Instance is TForm) then
begin
TForm(Instance).HandleNeeded;
FMainForm := TForm(Instance);
end;
end;
 
我知道在C++中这种特征是: 模板

 
呵呵,如果delphi代的source都搞懂了,这些就能弄明白了啊,
可惜我几乎没弄懂什么。
 
Richard3000,你的方法我没有试,还是前两位的方法好办,呵呵,多谢各位。
 
這是Delphi的source,不是我的.只是想給你的啟發.白大了![:(]
 
多人接受答案了。
 
顶部