New: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: New
| Old | New | Line | |
|---|---|---|---|
| 1 | + | Back to [[Reserved words]]. | |
| 2 | + | ||
| 3 | + | The reserved word **new**: | |
| 4 | + | ||
| 5 | + | - belongs to object-oriented programming; | |
| 6 | + | - creates a new instance. | |
| 7 | + | ||
| 8 | + | Example 1: | |
| 9 | + | ||
| 10 | + | ``` pascal | |
| 11 | + | var | |
| 12 | + | intI : ^ Integer ; | |
| 13 | + | ||
| 14 | + | begin | |
| 15 | + | ... | |
| 16 | + | if assigned ( intI ) then ... // Checks whether an address has been assigned to intI | |
| 17 | + | ... | |
| 18 | + | new ( intI ) ; // Assigns a valid address to intI | |
| 19 | + | ... | |
| 20 | + | dispose ( intI ) ; // Releases the address | |
| 21 | + | ... | |
| 22 | + | ``` | |
| 23 | + | ||
| 24 | + | Example 2: | |
| 25 | + | ||
| 26 | + | ``` pascal | |
| 27 | + | grade | |
| 28 | + | PListItem = ^ TListItem ; | |
| 29 | + | TListItem = record | |
| 30 | + | data : string ; | |
| 31 | + | next : PListItem ; // reference to the next element | |
| 32 | + | end ; | |
| 33 | + | ||
| 34 | + | var | |
| 35 | + | List : PListItem ; | |
| 36 | + | ||
| 37 | + | begin | |
| 38 | + | ... | |
| 39 | + | New ( list ^. Next ) ; // Adds a new item to the list | |
| 40 | + | List ^. next ^. data : = '' ; // empties the element | |
| 41 | + | List ^. next ^. next : = nil ; // sets the following pointer to nothing for security | |
| 42 | + | ... | |
| 43 | + | dispose ( list ) ; // Releases the list | |
| 44 | + | ... | |
| 45 | + | ``` | |
| 46 | + | ||
| 47 | + | --- | |
| 48 | + | *From the Free Pascal wiki, page "New", as it stood on 2020-02-21 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* |
