Lucas Steffen
Lucas Steffen

Reputation: 1364

What is this "cyclic inheritance" called in Delphi?

I saw a code like this the other day:

type
    TFather = class;
    TSon = class(TFather);
    TFather = class(TSon);
end;

Son inherited from Father that inherited from Son. Searched for Cyclic Inheritance but only Java results.

What is this called?

Upvotes: 0

Views: 222

Answers (4)

Rudy Velthuis
Rudy Velthuis

Reputation: 28806

I don't know what it is called, but it doesn't make sense. It is simply impossible.

If I try to compile this code:

type
  TFather = class;
  TSon = class(TFather)
  end;
  TFather = class(TSon)
  end;

I get:

[dcc32 Error] Project1.dpr(12): E2086 Type 'TFather' is not yet completely defined

and that is as expected.

Cyclic inheritance is an error condition in Java. You get a diagnostic mentioning "cyclic inheritance" if something like this is happening directly or indirectly (i.e. with a few classes inbetween). In other words, it is not a programming concept, it is an error condition.

Upvotes: 3

Rob Kennedy
Rob Kennedy

Reputation: 163287

The example code (that's now been deleted) doesn't do what you think it does. You see TMycxGridDBTableView being defined as a descendant of TcxGridDBTableView, and then you see TcxGridDBTableView, defined as a descendant of TcxGridDBTableView.

However, the TcxGridDBTableView you see at the top is not the same TcxGridDBTableView that you see later. The first one refers to a class declared elsewhere, in some other unit. The next occurrence is declaring a new class in this unit that happens to have the same base name as the other unit's class.

This technique is known as an interposer class. It's used to introduce a new GetViewDataClass method, but still end up with the same class name. The form that uses controls with that name will use the new version of the class instead of the original version. It's a way to customize a VCL control without having to compile and install a custom package.

Upvotes: 5

David Heffernan
David Heffernan

Reputation: 612993

What is this called?

The technical term is syntax error. This code is illegal.

Upvotes: 1

Dsm
Dsm

Reputation: 6013

Nonsense. You can have TSon with a property of type TFather, and TFather with a property TSon, but what you have written is pure nonsense.

The following makes some sense, but has no special term, since there is no recursion:

type
  TFather = class;
  TSon = class
    public
      Father : TFather;
  end;
  TFather = class
    public
       Son : TSon;
  end;

Upvotes: 4

Related Questions