Delphi2009 First Experience

Source: Internet
Author: User

First, the malpractice

First of all to thank netizens to assemble the head of the reminder, in my Delphi on the implementation of smart pointers in the first article "Delphi2009 first experience-language article-intelligent pointer Implementation" in the assembly head to me raised this question:

"It's a bit of a misnomer to call this a smart pointer, but there's a big difference between class-type object references and the semantics of pointers," he said. The smart pointers in C + + are a mechanism for semantic accessibility. ”

Then I thought, there was a problem with the Assembly head. In the original code, I made the following constraint:

Iautoptr<t:class>=interface (IInterface)

I set the T type to be a class type (Class), and if you use the TAUTOPTR encapsulation class TObject, then Tautoptr is only a "smart object", not "smart pointer." Here, we remove the T:class constraint class, where we can pass on the type of non class type, and of course the pointer type.

Ii. question-making

However, removing the constraint: Class will bring some problems:

First paste out the original code:

1 type
2 iautoptr<t:class> = Interface (iinterface)
3 [' {bd098fdb-728d-4cac-ae40-b12b8b556ad3} ']
4 function Get:t;
5 function Release:t;
6 procedure Reset (aobj:t);
7 End;
8
9 tautoptr<t:class> = Class (Tinterfacedobject, Iautoptr<t>)
Ten private
One fobj:t;
Public
class function New (aobj:t): iautoptr<t>;
Constructor Create (aobj:t); Virtual
destructor Destroy; Override
function Get:t;
the function release:t;
Procedure Reset (aobj:t);
End;
20
Implementation
22
{tautoptr<t>}
24
Constructor Tautoptr<t> Create (aobj:t);
At the Begin
Fobj: = Aobj;
End;
29
class function Tautoptr<t>. New (aobj:t): iautoptr<t>;
Begin
Result: = Tautoptr<t> Create (aobj) as iautoptr<t>; Note: If you do not add as Iautoptr<t&gt, the program will error when running, the first time I did not add as Iautoptr<t> program to run all normal, to the back of the end, do not know why
The end;
34
function Tautoptr<t>. Release:t;
The Begin
Panax Notoginseng Result: = Fobj;
Fobj: = nil;
The end;
40
Procedure Tautoptr<t> Reset (aobj:t);
The Begin
If Aobj <> Fobj Then
The Begin
Freeandnil (Fobj);
Fobj: = Aobj;
The end;
The end;
49
destructor Tautoptr<t> Destroy;
Wuyi
If Fobj <> Nil Then
Begin
Freeandnil (Fobj);
The end;
56
inherited;
The end;
59
the function Tautoptr<t>. Get:t;
The Begin
Result: = Fobj;
The end;

1, in the release method "Fobj: = nil", the compiler will not support, because fobj is the T type, t can be a value type, value type assignment to nil is not allowed.

2, in the Reset (aobj:t) method "Aobj <> fobj", the compiler will not support, although aobj and fobj are all t types, but generic t is any type, not all types support the "<>" comparison operator.

3. The "If Fobj = Nil then" in the Destroy method is not supported for the same reason as the 1th.

4, the "Freeandnil (fobj)" In the Destroy method is not supported, because t may be a value type, for the same reason as 1th.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.