Watch, Follow, &
Connect with Us
Public Report
Report From:    [ Add a report in this area ]  
Report #:  8521   Status: Reported
Cannot call constructors using instance variables
Project:   Build #:  7.1.1523.17956
Version:    8.0 Submitted By:   Peter Gummer
Report Type:  Basic functionality failure Date Reported:  6/28/2004 8:00:54 PM
Severity:    Infrequently encountered problem Last Updated:
Platform:    All platforms Internal Tracking #:  
Resolution: None  Resolved in Build: : None
Duplicate of:  None
Voting and Rating
Overall Rating: (1 Total Rating)
5.00 out of 5
Total Votes: 10
Description
In Delphi 1 to 7, this was legal:
    TObject.Create.Create;

In Delphi 8, the compiler complains, "Cannot call constructors using instance variables". Yet according to the on-line help it should still be legal:

ms-help://borland.bds2/bds2guide/html/Methods.htm:
"When a constructor is called using an object reference (rather than a class reference), it does not create an object. Instead, the constructor operates on the specified object, executing only the statements in the constructor's implementation, and then returns a reference to the object."

In Delphi 1 to 7, calling o.Create (where o is an object reference) reinitialised the same object o.

What seems to have changed in Delphi 8 is that:
1. Calls of the form o.Create (where o is an object reference) are now illegal.
2. Calls of the form Self.Create (where Self is an object reference) now create a new object of the same type as the old one.

Maybe the behaviour was deliberately changed in Delphi 8, but given that there is no documentation to this effect I can only assume that this is a bug in the compiler.
Steps to Reproduce:
This code demonstrates the behaviour in Delphi 8. All previous versions of Delphi behave completely differently.

type
  T1 = class
  public
    function Clone: T1;
  end;

function T1.Clone: T1;
begin
  Result := Create;
end;

function Test: string;
var
  o1, o2: T1;
begin
  o1 := T1.Create;
  o2 := o1.Clone;
  Result := o2.ClassType.ClassName;
  if o1 <> o2 then Result := Result + ' - different instances';
  // At this point, Result = 'T1 - different instances'
end;
Workarounds
None
Attachment
None
Comments

None

Server Response from: ETNACODE01