M
M
Mors Clamor2016-11-06 12:43:15
OOP
Mors Clamor, 2016-11-06 12:43:15

Pascal. Variables and arrays inside classes?

Good day. I'm doing a naval battle, I've screwed up on determining the destruction of the ship.
There is a field 10 by 10, there is an array that stores the number of live decks of each ship. These 2 arrays have to be passed to each class method as arguments, because it is not possible to pass them to the constructor and store them inside the class, the values ​​of the arrays do not change

......
type test=class
a:array[1..10, 1..10] of integer;
b:array [1..10] of integer;
constructor Create(a:array[1..10, 1..10] of integer; b:array [1..10] of integer);
...

Does not work. Is it possible to implement something similar? Thanks in advance

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mercury13, 2016-11-06
@Mercury13

What is happening? Doesn't compile?
It doesn't compile due to a misunderstanding of the concept of type equivalence. Unlike in C, seemingly identical types are not equivalent! For equivalence, it is necessary that their chains type A = B;lead to the same “ancestor”. There is an operator for this type.

const
  FieldSize = 10;
  MaxShips = 10;
type
  TField = record
    cells : array [1..FieldSize, 1..FieldSize] of integer;
    nLive : array [1..MaxShips] of integer;
  end;
  TGame = class
    Field : TField;
    constructor Create(const Field : TField);  
  end;

Perhaps the equivalence was weakened in Delphi, did not check. And that's how it is in BP.
You may have also made a well-known delphist novice mistake:
var
  x : Test;
....
x.Create(a, b);     // неверно!
x := Test.Create(a, b); // верно!

There is one exception to this type equivalence.
type
  DaInt = array of integer;

procedure DoSomething1(var x : array of integer);
procedure DoSomething2(var x : DaInt);

These commands both work, but are not equivalent!
The first is an innovation of TP7, an “open array” type parameter, an array of any dimension. Static, dynamic, 2D array string - everything will do. Low, High and (for D4+) Length are active.
The second is the D4 innovation, a dynamic array whose length can be changed via SetLength.

D
Daniil Demidko, 2016-11-07
@Daniro_San

If we are talking about PascalABC, then look at the documentation, it is a bit special.
If PascalABC.NET, then even more so, these are perverted lattices.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question