Entry
Delphi: Class: Friend: Can someone tell me what friend class is in Object Pascal?
Sep 25th, 2003 06:18
Knud van Eeden,
----------------------------------------------------------------------
--- Knud van Eeden --- 30 April 2002 - 08:46 pm ----------------------
Delphi: Class: Friend: Can someone tell me what friend class is in
Object Pascal?
In Object Pascal classes defined in the same unit are automatically
friends with each other -- once again the unit is much more than just a
file containing some definitions.
---
It can still be a bit strange for the C++ programmer, who thinks of
'private' as meaning 'not available to anyone unless the class
specifically grants permission'.
---
The approach in Object Pascal should work in the large majority of
designs, but you might imagine a few where the lack of a 'friend'
keyword could be akward.
[book: source: Entsminger, Gary - the way of Delphi - Prentice Hall -
ISBN 0-13-455271-7 - p. xxviii 'the friend keyword']
---
---
Data Fields
Data fields of a class are simply variables that are declared in the
class declaration; they can be considered as variables that have class
scope. Fields in classes are essentially the same as fields in records
except that their access can be controlled by declaring them as
private, public, or protected. Regardless of a field's access, it is
available for use in all methods of the class. Depending on the field's
access level, it can be visible outside the class as well. Private and
protected fields, for example, are private to the class and cannot be
seen outside the class. Public fields, however, can be accessed from
outside the class but only through an object. Take the TMyRect class
declared previously, for example. It has no public fields. You could
try the following, but you will get a compiler error:
Rect := TMyRect.CreateVal(0, 0, 100, 100);
Rect.Left := 20; { compiler error! }
The compiler error will say Undeclared identifier: `Left'.
The compiler is telling you that Left is a private field, and you can
not get to it. If Left were in the public section of the class
declaration, this code would compile.
---
NOTE:
The preceding discussion of private data fields holds true if the
TMyRect class were declared in a separate unit, but not true if the
TMyRect class is declared in the unit where it is used.
Classes
contained in the same unit have what are sometimes called friend
privileges, which means that the classes can access each other's
private data fields.
This applies only to classes declared in the same unit.
[book: source: - Teach Yourself Borland Delphi 4 in 21 Days - SAMS]
---
---
Internet: see also:
C#: Class: Friend: Can someone tell me what a friend class is in C#?
http://www.faqts.com/knowledge_base/view.phtml/aid/24737/fid/791
C++: Class: Friend: Can someone tell me what a friend class is in C++?
http://www.faqts.com/knowledge_base/view.phtml/aid/16164/fid/163
Java: Class: Friend: Can someone tell me what friend class is in Java?
http://www.faqts.com/knowledge_base/view.phtml/aid/16166/fid/165
----------------------------------------------------------------------