![]() ![]() It sounds like the reason is that a dynamic array is essentially just a pointer, and arr is syntactic sugar to dereference that pointer. The idea being that at runtime I can modify the UI just by selecting a different record. In these is a pointer to an array of strings. I am trying to set set up a system for populating a combobox based on const records. As a result, a properly-written ANSI C program will be accepted by a C++ compiler. It includes several improvements to C, plus many new features designed to support object-oriented programming, while still retaining the basic features of ANSI C. In my case, the A record was holding references to dynamic arrays, and it was those array elements that I was able to reassign through a constref parameter, which confused me: A = record I struggling with Pascal (using Lazarus) at the moment. C++ is a newer language built on the base of C by Bjarne Stroustrup in the early 1980s. In other words, should I use constref whenever I do not reassign a formal parameter and I want to enforce passing a record by reference, or only when that record is also truly immutable in the sense that the procedure does not write to any of its fields either? But also, what is the difference from the perspective of the compiler, since apparently it does not catch the fact that even with constref I am able to mutate fields of a record that is passed by reference?Īs the responder points out, the above sample code should (and does) fail to compile with fpc 3.x I had oversimplified. ![]() I was able to compile this just fine under fpc 3.0.4 with either var or constref, but my gut feeling is that rec should probably be declared as var here so as to indicate that the memory occupied by this record instance is changed by the procedure call? proc does not reassign rec, but it does perform a side-effect in that it mutates fields of its formal parameter. This distinction makes sense for simple types such as numbers and strings, but what about structured types? Assuming I have the following record: A = recordĪnd a procedure that acts on As like the following: procedure proc(var rec: A) Heres an example on how to use AnySort () function to sort an array of Integer. var suggests mutation of the argument is possible, while const does not.Both var and constref enforce that a parameter is passed by reference.I've been reading about the various means of passing parameters to procedures and functions, but I am still confused about the semantics of var and constref as they apply to records (this is regarding the Free Pascal dialect of the language, v3.x). ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |