Recent

Author Topic: Basic question about classes and objects  (Read 1354 times)

rwebb616

  • Full Member
  • ***
  • Posts: 115
Re: Basic question about classes and objects
« Reply #15 on: April 21, 2021, 03:21:59 pm »
You mean something like
Code: Pascal  [Select][+][-]
  1. ....
  2. Var MyWord1, MyWord2:TWord;
  3. ....
  4. MyWord1:=TWord.Create('cats','word',false);
  5. //DoSomething with MyWord1
  6. MyWord2:=MyWord1;
  7. //DoSomething with MyWord2
  8. MyWord1:=TWord.Create('dogs','word',false);
  9. //DoSomething with MyWord1
  10.  
  11. //Never once calling Free on any of the Variables
  12.  

Yes, I guess this is the same thing.. Looks like Martin answered it too..

rwebb616

  • Full Member
  • ***
  • Posts: 115
Re: Basic question about classes and objects
« Reply #16 on: April 21, 2021, 03:24:41 pm »

The pointer in the variable, and the instance in memory are 2 separate things.

- The pointer is controlled by := or list.add.
- The instance memory is controlled by Create/Destroy

Using :=/list.add does not change the instance memory.
And using Create/Destroy does not affect any pointers.


Ok then it sounds like I was doing everything right other than freeing the local variable (which I now understand was freeing the instance instead). 

Zvoni

  • Hero Member
  • *****
  • Posts: 544
Re: Basic question about classes and objects
« Reply #17 on: April 21, 2021, 03:32:31 pm »
Yes, Martin's answer is pretty comprehensive.

the concept of variables pointing to a memory-address, and what happens if you "loose" this reference....
I often have discussions with people not understanding that concept, and i try to explain it the following way:
"You have a bank-account, and that's where your money (physically) is. The Bank is your Memory/RAM, the safe with number 123456789 is the actual address of your safe within the bank."
"NOW, you have a credit-card associated with that bank-account of yours. And you carry that credit-card with you all around the world. But your credit-card is still pointing to that safe-number within your Bank, where it is stored"
"The credit-card is the variable, your safe 123456789 is your actual instance"
"What happens if you "loose" your credit-card? Correct! You cannot access your money anymore"


Curiously enough, pretty much everyone understands that......
One System to rule them all, One IDE to find them,
One Code to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
People call me crazy, because i'm jumping out of perfectly fine aircraft

rwebb616

  • Full Member
  • ***
  • Posts: 115
Re: Basic question about classes and objects
« Reply #18 on: April 21, 2021, 08:09:13 pm »
"What happens if you "loose" your credit-card? Correct! You cannot access your money anymore"

Yes, however losing my credit card (or keeping with the analogy, cutting up my credit card (destroy) ) doesn't orphan my bank account.  I can still get another credit card to access the same account.    :o

Rich

lucamar

  • Hero Member
  • *****
  • Posts: 3866
Re: Basic question about classes and objects
« Reply #19 on: April 21, 2021, 09:10:23 pm »
Yes, however losing my credit card (or keeping with the analogy, cutting up my credit card (destroy) ) doesn't orphan my bank account.  I can still get another credit card to access the same account.    :o

Because you have another "pointer" to that money: the address of the bank or a phone number or whatever, along with your identity card (or similar means).

Perhaps a better example would be one of those fabled anonymous, numbered swish accounts: if you loose the "accreditation", the money is left in "limbo" inside the bank.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

rwebb616

  • Full Member
  • ***
  • Posts: 115
Re: Basic question about classes and objects
« Reply #20 on: April 22, 2021, 01:09:34 am »
Here is a good example where I'm not sure how to handle it.  This is a function that returns a Tstringlist for use elsewhere:
Code: Pascal  [Select][+][-]
  1. function TData.GetCategories: TStringList;
  2. var
  3.   i : integer;
  4.   cats : TStringlist;
  5.   word : TWord;
  6. begin
  7.   cats := TStringlist.Create;
  8.   for i:=0 to Words.Count - 1 do begin;
  9.     word := Tword(words[i]);
  10.     if i = 0 then begin
  11.       cats.Add(word.Category);
  12.     end
  13.     else
  14.       if (cats.IndexOf(word.Category) = -1) then begin
  15.         cats.Add(word.Category);
  16.       end;
  17.   end;
  18.   Result := cats;
  19.   //cats := nil;
  20.   //cats.Free;
  21. end;

So I have Tdata instantiated as GameData in my main form and GameData is a global variable... so I can do:

Code: Pascal  [Select][+][-]
  1. var
  2.   myVar : TStringlist;
  3. Begin
  4.   myVar := GameData.GetCategories;
  5.  

As you can see I had commented out the cats:=nil and cats.free but if I call this procedure as shown... cats will be created for the function.  It will return the TStringList but then cats will go out of scope.  The stringlist will now be used by myVar so to clean up after myself would I just do myVar.free?

Sorry for all the questions - it's becoming more clear just need to get my head around this completely.

Rich

lucamar

  • Hero Member
  • *****
  • Posts: 3866
Re: Basic question about classes and objects
« Reply #21 on: April 22, 2021, 01:29:02 am »
This is easier and precludes the question:

Code: Pascal  [Select][+][-]
  1. function TData.GetCategories: TStringList;
  2. var
  3.   i : integer;
  4.   word : TWord;
  5. begin
  6.   Result := TStringlist.Create;
  7.   for i:=0 to Words.Count - 1 do begin
  8.     word := Tword(words[i]);
  9.     if i = 0 then begin
  10.       Result.Add(word.Category);
  11.     end
  12.   else
  13.       if (Result.IndexOf(word.Category) = -1) then begin
  14.         Result.Add(word.Category);
  15.       end;
  16.   end;
  17. end;

Could be even shorter, by setting the string list to reject duplicates and not using an intermediate var:

Code: Pascal  [Select][+][-]
  1. function TData.GetCategories: TStringList;
  2. var i : integer;
  3. begin
  4.   Result := TStringlist.Create;
  5.   Result.Duplicates := dupIgnore;
  6.   for i:=0 to Words.Count - 1 do
  7.     Result.Add(Tword(words[i]).Category);
  8. end;
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 7083
  • Debugger - SynEdit - and more
    • wiki
Re: Basic question about classes and objects
« Reply #22 on: April 22, 2021, 01:30:47 am »
Well to go for the banking analogy.

The instance is your bank account. (Or one of them if you have many)
.Create() means you go to the bank and open an account
.Destroy() means you close the account (same as Free)

When you open the account you get a card with it (credit/debit/whatever).
You can have any amount of cards. But of course to pay in a store, you need at least one card.

:= is a magic card copy maker ;)

   word := TWord.Create;
word is you banking card.

   foo := word;
Now you have 2 cards. You can use either. But it always affects the same account.

If you do
  word.Destroy
you close the account.
All the other cards also stop working.
Any attempt to use any of those card, will get you into trouble because payment fails.
You also are not allowed to attempt closing the account again (maybe by using another card to close it). The bank is really strict on that.

While the account is open, and so long as you always hold on to one card, you are fine. You can make new copies.

Also cards to not have to be named. They can be slots in a list.


As I said, once the account is closed, none of the cards work any more.

But they still look like cards. You even can still copy them (though the new copies will not work either).

So best practice is to cut the cards, and mark them as no longer working.
To cut a card
   word  := nil; // does not include closing the account. Cutting a working card only affects the one card.
or
    FreeAndNil(word) // includes closing the account. Also, only cuts this one card

If the account is closed, you have to find each card and cut it. If you don't, and you accidentally try to use it to pay, you go to jail. 
« Last Edit: April 22, 2021, 01:40:25 am by Martin_fr »

rwebb616

  • Full Member
  • ***
  • Posts: 115
Re: Basic question about classes and objects
« Reply #23 on: April 22, 2021, 04:28:09 am »

Could be even shorter, by setting the string list to reject duplicates and not using an intermediate var:

Code: Pascal  [Select][+][-]
  1. function TData.GetCategories: TStringList;
  2. var i : integer;
  3. begin
  4.   Result := TStringlist.Create;
  5.   Result.Duplicates := dupIgnore;
  6.   for i:=0 to Words.Count - 1 do
  7.     Result.Add(Tword(words[i]).Category);
  8. end;

I tried this and it looks like duplicates are being added... my checklistbox has a category entry for each item in each category so if there are 5 "cars" I see 5 cars entries in the category list. 

dupIgnore not working maybe?  I like the simplicity of this though! Thanks for your input!
Rich

rwebb616

  • Full Member
  • ***
  • Posts: 115
Re: Basic question about classes and objects
« Reply #24 on: April 22, 2021, 04:30:17 am »

If the account is closed, you have to find each card and cut it. If you don't, and you accidentally try to use it to pay, you go to jail.

I think I'm finally starting to get the idea!  Thank you for being patient and explaining things clearly.  My next study session is going to involve the generics that you and others have mentioned. 

Rich

egsuh

  • Hero Member
  • *****
  • Posts: 712
Re: Basic question about classes and objects
« Reply #25 on: April 22, 2021, 04:40:15 am »
Code: Pascal  [Select][+][-]
  1.     var
  2.       myVar : TStringlist;
  3.     Begin
  4.       myVar := GameData.GetCategories;
  5.  
  6.       // Do whatever necessary with myVar
  7.       // and you'd better free it when its use is over
  8.       myVar.Free;
  9.    end;
  10.  
         

Other guys gave full explanation. I'd like to add that you'd better have the habit of freeing created object when its use is over. It will take memory, if not freed.

Regarding dupIgnore, please check whether your texts are exactly the same --- case and leading or training blanks.

rwebb616

  • Full Member
  • ***
  • Posts: 115
Re: Basic question about classes and objects
« Reply #26 on: April 22, 2021, 04:54:03 am »
I figured it out - in order for Tstringlist.duplicates to have any effect the list must be sorted (TStringlist.sorted:=True)

Not sure yet whether I want my list sorted... hmm....
Rich

Thaddy

  • Hero Member
  • *****
  • Posts: 10784
Re: Basic question about classes and objects
« Reply #27 on: April 22, 2021, 09:08:29 am »
Not sure yet whether I want my list sorted... hmm....
Rich
Although technically possible, if the list is not sorted detecting duplicates can soon be impossibly slow.

dseligo

  • Full Member
  • ***
  • Posts: 231
Re: Basic question about classes and objects
« Reply #28 on: April 22, 2021, 09:35:44 am »
Well to go for the banking analogy.

What puzzles me in this analogy is this: Why do I have to close my account in the end (Free, Destroy) if I still have money on the account?
What will happen with my money (created object) then?  :)

Does bank leak money if accounts aren't closed (as programs leaks memory)?  :)

P.S.: OK, probably the owner of account dies in the end (program closes) and money goes to inheritants (OS).
« Last Edit: April 22, 2021, 09:37:15 am by dseligo »

egsuh

  • Hero Member
  • *****
  • Posts: 712
Re: Basic question about classes and objects
« Reply #29 on: April 22, 2021, 09:44:33 am »
Quote
What will happen with my money (created object) then?

This is a swiss bank account, and the bank will charge you for keeping the account open, and if you do not pay, Interpol will inspect you for keeping possible dark money.  :D

If the memory is not freed, it is regarded as taken and not available to other applications.
« Last Edit: April 22, 2021, 09:52:41 am by egsuh »

 

TinyPortal © 2005-2018