Given that the OP was asking about learning, maybe it would be worth mentioning that the way we normally have "data structures which hold different data" these days is by polymorphism in objects or classes. Instead of having an array of variant records, each of which might hold a description of a circle, square or pentagon depending on the value of a tag variable, we would now have an array of Shape, where Shape is a class instance or pointer to an object. The actual elements of the array would be Circle, Square or Pentagon, where these are types descended from Shape.

But if you look at it closely OP boils down to the same fundamental "pointers pointing to memory" logic too. You only don't tell the user. And that's something I hate with OP: You don't know what happens in detail (at least, it mostly isn't told you in any help about a programming language). I have a clear imagination about pointers pointing to memory addresses and variant records. But how are arrays of different objects (with different sizes) implemented? I suspect that it's simply an array of pointers pointing to the objects (spread all over the heap). But noone tells the users so they don't know exactly what happens under the hood and therefore may choose bad designs.

This has several benefits, not least the fact that new shapes can be added more easily than in the variant record case.

If you code this by yourself (array of pointers pointing to variant records) then it's the same as your OP design but at least you know how it is coded and where it can be optimized.


_______________________________________________
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply via email to