为什么Delphi XE3给出“E2382无法使用实例变量调用构造函数”? [英] Why Delphi XE3 gives "E2382 Cannot call constructors using instance variables"?

查看:565
本文介绍了为什么Delphi XE3给出“E2382无法使用实例变量调用构造函数”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的代码,在Delphi XE2编译,但不是在XE3,我不知道为什么。我已经把有问题的代码减少了一点点,并希望知道它在Delphi的意见是什么问题。试图在Delphi XE 2中编译包含此单元的项目工作正常,但是在Delphi XE3(试用版)中,它给出了[dcc32 Error] AffineTransform.pas(26):E2382无法使用实例变量调用构造函数。我知道这里唯一的偏心的事情是使用旧的对象类型,其中构造函数不是真正完全一样的东西,在真实对象(基于TObject的类实例)。

I have a simple piece of code, that compiles in Delphi XE2 but not in XE3, and I don't know why. I have reduced the problematic code to a small bit and would like to know what's wrong with it in Delphi's opinion. Trying to compile a project containing this unit in Delphi XE 2 works fine, but in Delphi XE3 (trial), it gives "[dcc32 Error] AffineTransform.pas(26): E2382 Cannot call constructors using instance variables". The only "eccentric" thing I know of here is the use of the old-school "object" type, where the constructor isn't really exactly the same thing as in real objects (TObject-based class instances).

如果我用'procedure'替换这个对象中的'constructor',那么它编译好了,但是为什么是这样,这是我的代码中的一个确定的改变,ie是不是对功能没有影响的改变?

If I replace the words 'constructor' in this object with 'procedure', then it compiles ok, but why is this, and is this an ok change to do in my code, i.e. is it a change that will have no effect on the functionality?

unit AffineTransform;

interface

type
  { Rectangular area. }
  TCoordRect = object
  public
    Left, Top, Right, Bottom: Real;
    constructor CreatePos(ALeft, ATop, ARight, ABottom: Real);
    procedure   Include(AX, AY: Real);
  end;

implementation

constructor TCoordRect.CreatePos(ALeft, ATop, ARight, ABottom: Real);
begin
  Left := ALeft;
  Top := ATop;
  Right := ARight;
  Bottom := ABottom;
end;

procedure TCoordRect.Include(AX, AY: Real);
begin
  CreatePos(AX, AY, AX, AY)
end;

end.


推荐答案

对于这种传统的Turbo Pascal风格 object ,关键字构造函数确实没有意义。虽然一个对象构造函数有一些特殊的处理,这里绝对没有必要。这里只有一个记录与一些方法。

For this legacy Turbo Pascal style object, there is really no meaning to the keyword constructor. Although an object constructor does have some special treatment, there's absolutely no need for that here. What have here is nothing more than a record with some methods.

XE3编译器更改, longer允许你在实例方法中调用 Self 上的构造函数。这是 class object 的情况。我没有看到任何文档,为什么这种变化。

The XE3 compiler was changed so that it no longer allows you to call a constructor on Self inside an instance method. That is the case for both class and object. I've not seen any documentation of why this change was made. No doubt in time it will seep out.

您的直接解决方案是用构造函数 > procedure 。从长远来看,将它转换为记录而不是对象是有意义的。

Your immediate solution is to replace constructor with procedure. In the longer term, it would make sense to turn this into a record rather than an object.

我也建议您将方法的名称更改为初始化。一些库设计者似乎选择对他们的记录使用创建自由方法。这导致大量的代码写成这样:

I would also council you to change the name of the method to Initialize. Some library designers seem to opt for using Create and Free methods on their records. This had led to immense amount of code being written like this:

ctx := TRttiContext.Create;
try
  ....
finally
  ctx.Free;
end;

事实上,所有的代码都是假的,可以简单的删除!

In fact all that code is spurious and can simply be removed! A TRttiContext variable will automatically initialize itself.

这样的设计也会设置一个巨大的 Heffalump陷阱对于喜欢使用 FreeAndNil 的Delphi程序员的派系。将记录传递给 FreeAndNil 会导致一些有趣的烟花!

That sort of design also sets a giant Heffalump Trap for that faction of Delphi coders that like to use FreeAndNil. Passing a record to FreeAndNil leads to some interesting fireworks!

这篇关于为什么Delphi XE3给出“E2382无法使用实例变量调用构造函数”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆