ВУЗ:
Составители:
Рубрика:
35
begin
result:=RVector.CreateEmpty;
for i:=1 to Dim do
result.comp[i]:=self.comp[i].Minus(v.comp[i]) as Rational;
end;
Поскольку конструктор CreateEmpty не размещает в памяти компоненты
вектора result, оператор присваивания делает именно то, что нужно —
адрес объекта, созданного методом Rational.Plus или Rational.Minus запи-
сывается в i-й элемент массива result.comp, до этого ничего не содер-
жавший. Утечки памяти при этом нет.
Последнее, что мы должны сделать, — это добавить к закрытому
интерфейсу класса RVector объявление конструктора CreateEmpty. О т-
метим , что это действие подпадает под пункт 6 общей схемы решения,
приведенной в задаче 2.1. Окончательный код модуля UnitRVector со все-
ми исправлениями содержит
Пример 2.3.10. Вторая версия модуля UnitRVector.
unit UnitRVector;
interface
uses UnitRational;
const Dim = 5;
type RVector = class
private
comp: array[1..Dim] of Rational;
constructor CreateEmpty;
public
function Plus (v: RVector): RVector;
function Minus (v: RVector): RVector;
function ScalarProduct(v: RVector): Rational;
function IsEqual (v: RVector): Boolean;
procedure Add (v: RVector);
procedure Subtract(v: RVector);
procedure Assign (v: RVector);
function GetLength: Real;
function GetComp(i: Integer): Rational;
procedure SetComp(i: Integer; value: Rational);
function AsString: String;
constructor Create;
destructor Destroy; override;
end;
implementation
uses SysUtils;
begin
result:=RVector.CreateEmpty;
for i:=1 to Dim do
result.comp[i]:=self.comp[i].Minus(v.comp[i]) as Rational;
end;
Пос кол ь ку конс т руктор CreateEmpty не раз м ещ ает в пам ят и ком понент ы
в ектора result, оператор прис в аив ания д ел ает им енно т о, что нужно —
ад рес объект а, с оз д анногом етод ом Rational.Plus ил и Rational.Minus з апи-
с ы в ает с я в i-й эл ем ент м ас с ив а result.comp, д о этого ничего не с од ер-
жав ший. Ут ечки пам ят и при этом нет.
Пос л ед нее, чтом ы д ол жны с д ел ат ь , — этод обав ит ь к з акры том у
инт ерфейс у кл ас с а RVector объяв л ение конс т руктора CreateEmpty. О т -
м ет им , что это д ейс т в ие под пад ает под пункт 6 общ ей с хем ы решения,
прив ед енной в з ад аче 2.1. О кончат ел ь ны й код м од ул я UnitRVector с ов с е-
м и ис прав л ениям и с од ержит
Пример2.3.10. Вторая в ерс ия м од ул я UnitRVector.
unit UnitRVector;
interface
uses UnitRational;
const Dim = 5;
type RVector = class
private
comp: array[1..Dim] of Rational;
constructor CreateEmpty;
public
function Plus (v: RVector): RVector;
function Minus (v: RVector): RVector;
function ScalarProduct(v: RVector): Rational;
function IsEqual (v: RVector): Boolean;
procedure Add (v: RVector);
procedure Subtract(v: RVector);
procedure Assign (v: RVector);
function GetLength: Real;
function GetComp(i: Integer): Rational;
procedure SetComp(i: Integer; value: Rational);
function AsString: String;
constructor Create;
destructor Destroy; override;
end;
implementation
uses SysUtils;
35
Страницы
- « первая
- ‹ предыдущая
- …
- 33
- 34
- 35
- 36
- 37
- …
- следующая ›
- последняя »
