Recent

Author Topic: [Solved] Objects and classes  (Read 7605 times)

ASerge

  • Hero Member
  • *****
  • Posts: 2223
Re: [Solved] Objects and classes
« Reply #15 on: March 20, 2017, 05:29:50 pm »
Well... try the code... It work....
Randomly lucky
Code: Pascal  [Select][+][-]
  1. program Project1;
  2. var
  3.   R: TObject = nil; // Default global initialization
  4. begin
  5.   R.Create; // No Self referencing
  6.   R.Free; // Special check Self for nil
  7. end.

sky_khan

  • Guest
Re: [Solved] Objects and classes
« Reply #16 on: March 20, 2017, 05:37:02 pm »
Well... try the code... It works....

Wow! Just wow!
I guess your mantra is "Ship it if it compiles".
Still I cant believe that you're serious and think this must be trolling.

Whatever
Congratulations! You're the one and only person on my ignore list.

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: [Solved] Objects and classes
« Reply #17 on: March 20, 2017, 05:55:22 pm »
Well... try the code... It works....

Wow! Just wow!
Great! Now add the idiots that didn't try the code and have a habit of not knowing anything and just assume.....
Grow up and TEST like I do. I would not have much confidence in people who - without even trying perfectly working code - put you on the wrong track.
Test the code. It works. He's wrong.

Note it may still be a bug. Not that my code works but that the prick has code that doesn't work.
Specialize a type, not a var.

Fungus

  • Sr. Member
  • ****
  • Posts: 353
Re: Objects and classes
« Reply #18 on: March 20, 2017, 07:25:12 pm »
Code: Pascal  [Select][+][-]
  1. program objectarray;
  2. {$mode objfpc}
  3. type
  4.   TCard =class
  5.   end;
  6. var
  7.   a:array[0..29] of TCard;
  8.   c:TCard;
  9. begin
  10.   for c in a do c.create;
  11.   // and of course
  12.   for c in a do c.free;
  13. end.

I've just tested this code with FPC 3.0.0 and it does obviously not work (SIGSEGV / access violation of address 0 / nil pointer reference). Even if FPC 3.0.2 version supports this malformed code I'd consider the example to be wrong. The correct way which will work with all pascal compilers is:

Code: Pascal  [Select][+][-]
  1. var I: Integer;
  2.     ...
  3. begin
  4.   for I:= Low(a) to High(a) do a[I]:= TCard.Create;
  5.   ...
  6.  
« Last Edit: March 20, 2017, 07:27:54 pm by Fungus »

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: [Half Solved] Objects and classes
« Reply #19 on: March 20, 2017, 08:27:29 pm »
Code: Pascal  [Select][+][-]
  1. var
  2.   a:array[0..29] of TCard;
  3.   c:TCard;
  4. begin
  5.   for c in a do c.create;
Assigning to an array element in this way is basically the SAME as assigning to a variable, which just happens to be an array element.
I disagree. No array elements are assigned by this construct.
The enumerator merely provides a series of integers (the enumerator's Current property) from Low(a) to High(a) as indices, so that the loop can call Create on each indexed array element in turn (i.e. on 30 nil values, since global arrays are initialized to zero values; or on 30 WriteLn calls in the next line of your code).

In this "for .. in .. do" syntax sugar, c is a placeholder 'variable'. It cannot be written to, nor can it be the recipient of the result of a  constructor call. I think it appears in the syntax principally to ensure that the correct enumerator is called (in case there are two or more enumerators defined for the class or array in question).

 

TinyPortal © 2005-2018