Thread View: cn.bbs.comp.language
1 messages
1 total messages
Started by �Ϲ⡤��������
Wed, 13 Mar 2002 19:01
��̸�쳣����̸C++��Object Pascal�еĹ��캯������
Author: �Ϲ⡤��������
Date: Wed, 13 Mar 2002 19:01
Date: Wed, 13 Mar 2002 19:01
150 lines
7249 bytes
7249 bytes
Nicrosoft��ԭ����
�ؼ��� �쳣 c++ object pascal
��������֪������Ĺ��캯����û�з���ֵ�ģ�����캯�������ʧ�ܣ���������
�����ش�����롣��ô���ڳ�������α�ʶ���캯���ʧ���أ�������ķ�������
���׳�һ���쳣��
�������캯��ʧ�ܣ���ζ�Ŷ���Ĺ���ʧ�ܣ���ô�׳��쳣֮����������������
����ᱻ��δ����أ�����DZ��ĵ����⡣
������C++�У����캯���׳��쳣����������ᱻ���á����Ǻ���ģ���Ϊ��ʱ����
��û�б������졣Ҳ����˵������캯���Ѿ�����һЩ��������ڴ桢���ļ���
�����Ļ�����ô����Ҫ���Լ��ij�Ա����ס������Щ��������C++�У�����Ľ��������
ʹ��STL�ı���auto_ptr������ÿһ������C++�����ж��н��ܣ���������Ͳ���˵��
������������ٽ���һ�֡��dz��桱�ķ�ʽ����˼����DZ����ڹ��캯�����׳��쳣
�����ǿ�������������һ�� Init(); �Լ� UnInit();��Ա�������ڽ������ײ�������
��Դ���乤����������Ĺ��캯�����Ƚ����г�Ա��ΪNULL��Ȼ����� Init(); ���ж�
�䷵��ֵ�����߲� Init()�׳����쳣�������Init();ʧ���ˣ����ڹ��캯���е���
UnInit(); ������һ����־λ��������ʧ�ܡ�UnInit()�а��ճ�Ա�Ƿ�ΪNULL������Դ
���ͷŹ�����ʾ��������£�
class A
{
private:
char* str;
int failed;
public:
A();
~A();
int Init();
int UnInit();
int Failed();
};
A::A()
{
str = NULL;
try
{
Init();
failed = 0;
}
catch(...)
{
failed = 1;
UnInit();
}
}
A::~A()
{
UnInit();
}
int A::Init()
{
str = new char[10];
strcpy(str, "ABCDEFGHI");
throw 10;
return 1;
}
int A::UnInit()
{
if (!str)
{
delete []str;
str = NULL;
}
printf("Free Resource\n");
return 1;
}
int A::Failed()
{
return failed;
}
int main(int argc, char* argv[])
{
A* a = new A;
if ( a->Failed() )
printf("failed\n");
else
printf("succeeded\n");
delete a;
getchar();
return 0;
}
������ᷢ�֣���int A::Init()�а���throw 10;�Ĵ��루����һ���쳣��ģ�����
�ķ���ִ�н���ǣ�
����Free Resource
����failed
����Free Resource
������Ȼ UnInit();�����������Σ���������UnInit();�������жϣ�if (!str)������
�˲��ᷢ��������û�з����쳣��ȥ�� int A::Init()�е�throw 10;���룩��ִ
�н���ǣ�
����Succeeded
����Free Resource
�������������û���κ����
������Object Pascal��Delphi/VCL���У��������ͱ�÷dz��ļ��ˣ���Ϊ OP �Թ�
�캯����쳣�Ĵ�����C++��ͬ����Createʱ�׳��쳣���������Զ�������������D
estroy�����һ��ж���Щ��Դ�������ˣ�ʵ���Զ����ա���ˣ������Ҳ��÷dz����
�����£�
type
A = class
private
����str : PChar;
public
����constructor Create();
����destructor Destroy(); override;
end;
constructor A.Create();
begin
����str := StrAlloc(10);
����StrCopy(str, 'ABCDEFGHI');
����raise Exception.Create('error');
end;
destructor A.Destroy();
begin
����StrDispose(str);
����WriteLn('Free Resource');
end;
var oa : A;
����i : integer;
begin
����try
���� oa := A.Create();
���� WriteLn('Succeeded');
���� oa.Free();
����except
���� oa := nil;
���� WriteLn('Failed');
����end;
����Read(i);
end.
��������δ����У�����캯���׳��쳣����Create�к���raise Exception.Create
('error');����ִ�еĽ���ǣ�
����Free Resource
����Failed
������ʱ�ġ�Free Resource��������ɱ������Զ������������������ġ��������
������أ������׳��쳣������ִ�н���ǣ�
����Succeeded
����Free Resource
������ʱ�ġ�Free Resource��������� oa.Free()�ĵ��ò���ġ�
�������ϣ�C++��Object Pascal���ڹ��캯���׳��쳣��IJ�ͬ���?ʽ����ʵ������
�����Ե����˼������֡�C++���C�ķ��ע��Ч�ʣ�һ�н������Ա�����գ�����
���������ද����Object Pascal�̳�Pascal�ķ��ע�س������ѧ���壨���ɷ��ϣ�
Pascal������ȫ�����������Ĵ��룩���������������Ա��ɸ��ӵĹ������������Զ�
�д��ڵ����ɣ����д��ڵı�Ҫ������������֮��IJ���������õؿ������ǣ���
�����ɵ���������
�Ը��ĵ�����
flysky66 (2001-9-16 19:17:08)
����
--
MM MM ��MM��MM����ࣻ
�罫MM�����³����ɡ�
[m[1;32m�� ��Դ:���콭ҹ�� bbs.gliet.edu.cn��[FROM: 192.168.89.122][m
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads