constructor TForm2.Create(AOwner: TComponent);
begin
inherited;
if (n >0) then
raise Exception.Create('error');
n := n+1;
end;destructor TForm2.Destory;
begin
n := n-1;
end;procedure TForm1.Button1Click(Sender: TObject);
var
Form:TForm2;
begin
Form := TForm2.Create(Application);//第二次执行就会报错。
Form.SHow;
end;
begin
inherited;
if (n >0) then
raise Exception.Create('error');
n := n+1;
end;destructor TForm2.Destory;
begin
n := n-1;
end;procedure TForm1.Button1Click(Sender: TObject);
var
Form:TForm2;
begin
Form := TForm2.Create(Application);//第二次执行就会报错。
Form.SHow;
end;
解决方案 »
- 连定义的STRING也有释放内存?
- delphi中用ado连接Sybase驱动是Sybase.ASEOLEDBProvider.2
- Pointer无类型指针能否指向一个对象???
- 请教有关Delphi QuickReport的问题
- 怎么用ado中的 adodataset 将多张excel表格导入SQL中!!!!!!!!!!!!!!!!!!!!!!!!!!!
- Delphi技术论坛QQ群组
- 找不到 system.pas ? 请帮个忙!
- 有关指定窗体上面的控件的句柄?
- COMBOBOX的宽度不够怎么办?
- 请问如何用SQL语句计算相同条件的值的合并把该合赋给一个变量
- 初学delphi,关于ADO问题一问。
- 如何处理strtofloat()格式转化的异常?
我主要是想知道如何实现Singleton这个模式,而不是Dephi中如何用类方法
http://community.borland.com/article/0%2C1410%2C22576%2C00.html
>>这种方法只能在本单元创建类的实例,在别的单元就不行了
谁说的?完全可以在其他单元创建。
如TMyclass
public
MyObj: TMyClass
然后自定义构造函数
if MyObj<>nil then
Result:=MyObj.Create;
类本身的Create方法不对外发布
我理解错你的意思了。但我还是觉得这是个不好的方法
我主要是想讨论问题。分我一定会给的。
你可以查看我提问的问题,到目前为止只有这一个帖子没有结帐
还有,你说的TStatic类在哪里?我用的是Delphi 6,找不到这个类
begin
TempPrinter := TPrinter.Create;
FList.Items[I] := TempPrinter;
end;
自己封装这个类,关键是用指针,自己申请对象的空间,然后自己维护,
至于singleton的实现方式,还是我上面说过的方法,
只不过成员变量得用指针形式,共享空间
另外,分其实无所谓
1、屏蔽掉原来的构造方法,而用NewInstance来创建实例。
2、用一个全局变量来作为引用计数器。每创建一个实例,计数器就加一;每释放一个实例,计数器就减一。当计数器为0时,真正销毁实例。(类似COM组件)
public
function ISObjectExists:Boolean;
constructor create;
end;
constructor TSingleton.create;
begin
if ISObjectExists then
begin
MessageDlg('对象已存在,退出!', mtError, [mbOK], 0);
exit;
end
else
begin
inherited;
end;
end;function TSingleton.ISObjectExists:Boolean;
var
hMutex : THandle;
begin
Result := False;
hMutex := CreateMutex(nil,False,'Singleton');
if GetLastError= ERROR_ALREADY_EXISTS then
begin
Result := True;
ReleaseMutex(hMutex);
end;
end;
这样能满足要求吗?