I am but a beginner in FreePascal. Please correct where necessary.
I see a number of issues in the replies.
- Encapsulation. It is not "the ability to have all related parts in one container". It is about restricting access to data and functions that work on that data. ('functions' used in a general 'working' sense) Sure, they are related because the functions work on that data. However, the emphasis is on hiding.
- Terminology seems to be used very loose.
The term 'File' shouldn't be used when talking about the program structure.
A standard 'Unit' is a container providing encapsulation. It may even be looked like a 'singleton' which is a class that has exactly one instance. IE: not a 'file'. Has the same filename though.
A unit containing a single class: Here the class provides the encapsulation, not the unit. It is still not a 'file' though.
From program structure point of view 'file' doesn't apply. File applies to units if looking the unit from the OS's file-system point of view.
- Putting several classes into a single unit is not encapsulation. In this case the unit is just a 'container'. (sort of ... )
- If a class's functions are distributed between different units, it is not braking the encapsulation. May be poor practice though.
- Making functions/data public that shouldn't be is breaking the encapsulation even if it is in a class. Distributing code - that should be hidden - into several standard units is breaking encapsulation.
- Encapsulation does not apply to the original question. The question was about cross-referencing between two unrelated classes. (Relation meant in the sense of inheritance. Naturally, they are 'related' in the sense that they work towards the same goal in the same program)
- Putting unrelated classes into the same unit. Consider this:
A Car 'is-a' Vehicle.
A Truck 'is-a' Vehicle.
Both Car and Track inherits from Vehicle.
A Car 'has-a' Engine.
The Truck 'has-a' Engine.
Both Car and Truck needs to know about it's engine.
Also, the Engines have to know to which Car/Truck they belong to.
We have now two cross-references.
Are we going to put all the Vehicle-derivatives AND ALL THEIR SHARED PARTS into the same unit?
Do I understand correctly, this is the official way?
Or everything is a TObject and casting endlessly ...
Thanks! Cheers!