-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathfriendfun.yo
More file actions
75 lines (71 loc) · 3.14 KB
/
friendfun.yo
File metadata and controls
75 lines (71 loc) · 3.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
In section ref(EXTRACTORS) the insertion operator of the class
tt(Person) (cf. section ref(ASSIGNMENT)) was implemented like this:
verb(
ostream &operator<<(ostream &out, Person const &person)
{
return
out <<
"Name: " << person.name() << ", "
"Address: " << person.address() << ", "
"Phone: " << person.phone();
}
)
tt(Person) objects can now be inserted into streams.
However, this implementation required three member functions to be called,
which may be considered a source of inefficiency. An improvement would be
reached by defining a member tt(Person::insertInto) and let tt(operator<<)
call that function. These two functions could be defined as follows:
verb(
std::ostream &operator<<(std::ostream &out, Person const &person)
{
return person.insertInto(out);
}
std::ostream &Person::insertInto(std::ostream &out)
{
return
out << "Name: " << d_name << ", "
"Address: " << d_address << ", "
"Phone: " << d_phone;
}
)
As tt(insertInto) is a member function it has direct access to the
object's data members so no additional member functions must be called when
inserting tt(person) into tt(out).
The next step consists of realizing that tt(insertInto) is only defined
for the benefit of tt(operator<<), and that tt(operator<<), as it is declared
in the header file containing tt(Person)'s class interface should be
considered a function belonging to the class tt(Person). The member
tt(insertInto) can therefore be omitted when tt(operator<<) is declared as a
friend.
Friend functions must be declared as friends in the class interface. These
hi(friend: function declaration) em(friend declarations) are not em(member)
functions, and so they are independent of the class's tt(private, protected)
and tt(public) sections. Friend declaration may be placed anywhere in the
class interface. Convention dictates that friend declarations are listed
directly at the top of the class interface. The class tt(Person), using
tt(friend) declaration for its extraction and insertion operators starts like
this:
verb(
class Person
{
friend std::ostream &operator<<(std::ostream &out, Person &pd);
friend std::istream &operator>>(std::istream &in, Person &pd);
// previously shown interface (data and functions)
};
)
The insertion operator may now directly access a tt(Person) object's data
members:
verb(
std::ostream &operator<<(std::ostream &out, Person const &person)
{
return
cout << "Name: " << person.d_name << ", "
"Address: " << person.d_address << ", "
"Phone: " << person.d_phone;
}
)
Friend declarations are true declarations. Once a class contains friend
declarations these friend functions do not have to be declared again below the
class's interface. This also clearly indicates the class designer's intent:
the friend functions are declared by the class, and can thus be considered
functions belonging to the class.