需要我把重载或覆盖的话在构造函数声明后的派生类? [英] Need I to put overload or override words after the constructor declaration in derived class?

查看:161
本文介绍了需要我把重载或覆盖的话在构造函数声明后的派生类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类层次结构,这一个:

I have a class hierarchy, this one:

type
TMatrix = class
    protected
      //...
    public
      constructor Create(Rows, Cols: Byte);
    //...
type
  TMinMatrix = class(TMatrix)
    private
      procedure Allocate;
      procedure DeAllocate;
    public
      constructor Create(Rows, Cols: Byte);
      constructor CreateCopy(var that: TMinMatrix);
      destructor Destroy;
  end;

正如你所看到的,派生类和基类构造函数都有相同的参数列表。
我从显式调用基类构造函数:

So as you see, both derived and base class constructors have the same parameter list. I explicitly call base class constructor from derived one:

constructor TMinMatrix.Create(Rows, Cols: Byte);
begin
   inherited;
   //...
end;

是否有必要在Delphi中显式调用基类构造函数?可能是我需要重载或覆盖来清除我打算做什么?我知道如何在C ++中 - 你需要显式调用基类构造函数,只有当你想传递一些参数 - 但我没有在Delphi编程的经验。

Is it necessary to explicitly call base class constructor in Delphi? May be I need to put overload or override to clear what I intend to do? I know how to do it in C++ - you need explicit call of a base class constructor only if you want to pass some parameters to it - but I haven`t much experience in Delphi programming.

推荐答案

据我所知,这里有两个独立的问题:

As far as I know, there are two separate issues here:

您必须显式地调用基类的构造函数:

You'll have to explicitly call the base class' constructor:

constructor TMinMatrix.Create(Rows, Cols: Byte);
begin
   inherited;
   //...
end;



确保子类的构造函数覆盖基类的构造函数



您还将 必须使子类的构造函数 override 和基类的构造函数 virtual ,以确保编译器看到两者之间的关系。如果你不这样做,编译器可能会警告你TMinMatrix的构造函数是隐藏TMatrix的构造函数。所以,正确的代码是:

Making sure the child class' constructor overrides the base class' constructor

You'll also have to make the child class' constructor override, and the base class' constructor virtual, to make sure the compiler sees the relation between the two. If you don't do that, the compiler will probably warn you that TMinMatrix's constructor is "hiding" TMatrix's constructor. So, the correct code would be:

type
TMatrix = class
    protected
      //...
    public
      constructor Create(Rows, Cols: Byte); virtual;    // <-- Added "virtual" here
      //...
type
  TMinMatrix = class(TMatrix)
    private
      //...
    public
      constructor Create(Rows, Cols: Byte); override;   // <-- Added "override" here
      constructor CreateCopy(var that: TMinMatrix);
      destructor Destroy; override;                     // <-- Also make the destructor "override"!
  end;

注意,你也应该使你的析构函数 override

Note that you should also make your destructor override.

请注意,您只能覆盖具有相同参数的构造函数列表。如果一个子类需要一个具有不同参数的构造函数,并且你想阻止基类的构造函数被直接调用,你应该写:

Note that you can only override a constructor with the same parameter list. If a child class needs a constructor with different parameters, and you want to prevent the base class' constructors from being called directly, you should write:

type
TMyMatrix = class(TMatrix)
//...
public
  constructor Create(Rows, Cols, InitialValue: Byte); reintroduce; virtual;
//...
end

implementation

constructor TMyMatrix.Create(Rows, Cols, InitialValue: Byte);
begin
  inherited Create(Rows, Cols);   // <-- Explicitly give parameters here
  //...
end;

我希望这可以使事情更清楚...祝你好运!

I hope this makes things more clear... Good luck!

这篇关于需要我把重载或覆盖的话在构造函数声明后的派生类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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