Class of variable calls incorrect constructor

If I have code like this:

TOne = class
constructor create;
end;

TOneClass = class of TOne;

TTwo = class(TOne)
constructor create;
end;

TThree = class(TOne)
constructor create;
end;

Then I have a variable:

FItemClass : TOneClass;

and that variable is assigned:

FItemClass := TTwo;

then calling FItemClass.new calls the constructor of TOne, not the Constructor of TTwo. In version 4 this worked as expected. If there is a workaround, I need it urgently otherwise I’m going to have to go back to using version 4.

Regards
Keith

Thanks, logged as bugs://54664 for review
Posted by Bugs for Mac

@kgiddings: Shouldn’t you use virtual/override here?

Hi Carlo,

Sorry for the delay in replying, I was on holiday.

I don’t think it changes anything as I’m pretty sure I tried that, but after the holiday, I’m no longer sure :slight_smile: so I’ll set up a test to see if that fixes it. It definitely worked without it being virtual and overridden in version 4.

Regards
Keith

I don’t think virtual/override of something changes the fact that the FItemClass.new should have created an object of the proper class and didn’t. FWIW.

Hi Carlo,

I ran a test using virtual/override and it doesn’t make any difference.

Regards
Keith

I just tried:

unit issue54664test;
interface

uses
  System.Linq, System.Collections.Generic;

type
  ConsoleApp = public class
  public
    class method Main(args: array of string): Integer;
  end;
TOne = class
constructor ; virtual;empty;
end;

TOneClass = class of TOne;

TTwo = class(TOne)
constructor ; override;empty;
end;

TThree = class(TOne)
constructor ; override; empty;
end;


implementation

class method ConsoleApp.Main(args: array of string): Integer;
begin
var FItemClass : TOneClass;
FItemClass := TTwo;
var x := new FItemClass();
Console.WriteLine(x);
Console.WriteLine(x.GetType);
end;

end.

Which gives:
issue54664test.TTwo
issue54664test.TTwo

Without the virtual/override i get:
issue54664test.TOne

what am I missing?

Sorry for the delay, I’ve been busy on another project.

I used your project and took away the constructor in class TTwo. This shows up the error. If my class doesn’t do anything additional in the overridden constructor, I shouldn’t need to have it there should I ?

I think though that it shouldn’t need to be virtual/overridden anyway as it should construct a class of the type held by the class variable.

Regards
Keith

Ah. The automatically added constructors should be override if the parent’s were override too. Logged as bugs://55494. Every class has constructors in Oxygene. Else you cannot instantiate them. If you don’t add them it adds them for you, now those should probably be override automatically.