А ты - по ссылке, которую я давал раньше, там все написано
Ничего к сказанному там я добавить не могу...
type
PT = ^T;
T = object
constructor create;
destructor done;{добавляем деструктор}
end;
destructor T.done;{который ничего не делает}
begin
end;
procedure TArr.count_each(const p: PT);
var i, count: integer;
begin
count := 0;
for i := 1 to maxSize do
if (arr[i]<>nil) and (typeof(arr[i]^) = typeof(p^))
then inc(count);
writeln('count = ', count);
dispose(p,done);{------------свобождаем память}
end;
var
obj_int: PTint;
obj_float:PTfloat;
any_arr: TArr;
mem:longint;
begin
clrscr;
mem:=memavail;{замеряем память до начала свалки}
any_arr.init;
obj_int := new(PTint,create);
obj_int^.value := 3;
any_arr.set_index(1, obj_int);
obj_float := new(PTfloat,create);
obj_float^.value := 3.5;
any_arr.set_index(2, obj_float);
any_arr.count_each(new(PTfloat,create));
dispose(obj_float,done);{чистим за собой}
dispose(obj_int,done);{и это тоже}
writeln('Difference : ',mem-memavail);{вроде так как и было...}
readln;
end.
procedure TArr.set_index(i: integer; p: PT);
begin if i <= maxSize then arr[i] := p; end;
any_arr.set_index(1,new(PTfloat,create));
constructor TArr.init;{добавим некую полезную нагрузку в конструктор}
var i:byte;
begin
for i:=1 to maxSize do {обnilим все элементы}
arr[i]:=nil;
end;
procedure TArr.set_index(i: integer; const p: PT);
begin
if i<= maxSize then
begin
if arr[i]=nil then
arr[i]:=p
else
begin
dispose(arr[i],done);
arr[i]:=p;
end;
end;
end;
function TArr.set_index(i: integer; const p: PT):boolean; {сделаем функцию вместо процедуры
true - все нормально
false - наоборот}
begin
if i<= maxSize then
if arr[i]=nil then
begin
arr[i]:=p;
set_index:=true;
end
else
set_index:=false;
end;
type
TArr = object
arr: array[1 .. maxSize] of PT;
num:byte;{сколько объектов уже было добавлено}
procedure set_index(const p: PT);{i нам больше не понадобится}
end;
constructor TArr.init;
var i:byte;
begin
num:=0;{!!!!!!!!!!!!!!!!!}
for i:=1 to maxSize do
arr[i]:=nil;
end;
procedure TArr.set_index(const p: PT);
begin
if num< maxSize then
begin
inc(num);
arr[num]:=p;
end;
end;
obj_int := new(PTint,create);
obj_int^.value := 3;
any_arr.set_index(1, obj_int);
any_arr.set_index(1, obj_float);
begin
dispose(arr[i],done);
arr[i]:=p;
end;
dispose(obj_int,done);
any_arr.count_each(new(PTfloat,create));
typeне напечатает ничего? Как же она может напечатать, если я передаю (да еще и константно) указатель на объект, который НЕ существует? Вынужден тебя огорчить, она будет работать... Объясни тогда, почему, если НЕТ указателя?
pT = ^T;
T = object
value: integer;
constructor init(x: integer);
procedure print; virtual;
end;
constructor T.init(x: integer);
begin value := X; end;
procedure T.print;
begin writeln(value); end;
procedure print_me(const p: pT);
begin
p^.print;
end;
begin
print_me(new(pT, init(20)));
print_me(new(pT, init(30)));
print_me(new(pT, init(40)));
end.
procedure TArr.count_each(const p: PT);Что должно быть на месте вопросов, чтобы внутри count_each произошла Run-Time ошибка 210 ? И где именно она может произойти (желательно показать строчку внутри процедуры, и сказать, ЧТО эту ошибку может вызвать)...
var i, count: integer;
begin
count := 0;
for i := 1 to num do
if (typeof(arr[i]^) = typeof(p^))
then inc(count);
writeln('count = ', count);
dispose(p,done);
end;
...
any_arr.count_each( ??? );
typeof(p^)
var
obj_int: PTint;
p:pointer;
begin
obj_int := new(PTint,create);
p:=obj_int;
any_arr.count_each(p);
end.
any_arr.count_each(nil);
function TArr.set_index(const p: PT):boolean;- полностью твой код, у меня этого просто не было... А теперь представь, что мне надо первые 10 элементов установить в значение 3.5 - неужели для этого я должен инициализировать переменную 10 раз... Я сделал так:
begin
inc(num);
if num<= maxSize then
begin
arr[num]:=p;
set_index:=true;
end
else
begin
dec(num);
dispose(p,done);
set_index:=false;
end;
end;
obj_float := new(PTfloat,create);и что я получу, догадайся?
obj_float^.value := 3.5;
for i:=1 to 10 do
begin
if obj_float <> nil then { <--- Вот эту проверку, кстати, пользователь, делать не должен ... }
any_arr.set_index(obj_float);
end;
typeМожно ли добиться того, чтобы при каких-то условиях было выведено сообщение "not assigned", и если ответ = "да", то перечисли все способы, которые ты можешь себе представить для этого (ничего не меняя внутри самой процедуры X, только снаружи), а если "нет" - то почему?
pT = ^T;
T = object
...
end;
procedure X(const the_pointer: pT);
begin
if assigned(the_pointer) then writeln('not assigned');
end;
var
p: ^T;
...
X( {что-то} )
...
p:^T
type
T = object
b:byte;
end;
2 Создать ПВТ.
type
T = object
constructor init;
end;
constructor t.init;
begin end;
type
T = object
destructor done;
end;
destructor t.done;;
begin end;
type
T = object
procedure VirtualMethod; virtual;
end;
procedure T.VirtualMethod;
begin end;
var
p: ^T;
begin
new(p);
X(p);
end.
procedure X(const the_pointer: pT);
begin
if NOT assigned(the_pointer) then writeln('not assigned');
end;
type
pT = ^T;
T = object
end;
procedure X(const the_pointer: pT);
begin
if not(assigned(the_pointer)) then writeln('not assigned');
end;
var
p: ^T;
begin
X(nil);
end.
type
pT = ^T;
T = object
end;
procedure X(const the_pointer: pT);
begin
if the_pointer=nil then writeln('not assigned');
end;
var
p: ^T;
begin
X(p);
readln;
end.
type
pT=^t;
T=object
constructor init;
end;
constructor T.init;
begin end;
procedure X(const the_pointer:pT);
begin
if the_pointer=nil then writeln('not assigned');
end;
var p:pointer;
ob:t;
begin
p:=@p;{свяжем p с любым адресом}
getmem(p,sizeof(ob));{p:=nil}
x(p);
readln;
end.