File: normalfriends.yo

package info (click to toggle)
c%2B%2B-annotations 13.02.02-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,576 kB
  • sloc: cpp: 25,297; makefile: 1,523; ansic: 165; sh: 126; perl: 90; fortran: 27
file content (48 lines) | stat: -rw-r--r-- 2,847 bytes parent folder | download | duplicates (4)
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
    A class template may declare an ordinary function, ordinary member
function or ordinary class as its friend.  Such a i(friend) may access the
class template's i(private members).

    Concrete classes and ordinary functions can be declared as friends, but
before a single member function of a class can be declared as a friend, the
compiler must have seen the i(class interface) declaring that member. Let's
consider the various possibilities:
    itemization(
    it() A class template may declare an ordinary function to be its
friend. It is not completely clear em(why) we would like to declare an
ordinary function as a friend. Usually we pass an object of the class
declaring the friend to such a function. With class templates this requires us
to provide the (friend) function with a template parameter without specifying
its types. As the language does not support constructions like
        verb(void function(std::vector<Type> &vector))

unless tt(function) itself is a template, it is not immediately clear how
and why such a friend should be constructed. One reason could be to allow the
function access to the class's private static members. In addition such
friends could instantiate objects of classes that declare them as their
friends. This would allow the friend functions direct access to such object's
private members. For example:
        verbinclude(//FUNCTION examples/concretefriends.cc)
    it() Declaring an ordinary class to be a class template's friend probably
finds more applications. Here the ordinary (friend) class may instantiate any
kind of object of the class template. The friend class may then access all
private members of the instantiated class template:
        verbinclude(//CLASS examples/concretefriends.cc)
    it() Alternatively, just a single member function of an ordinary class may
be declared as a friend. This requires that the compiler has read the friend
class's interface before declaring the friend. Omitting the required
destructor and overloaded assignment operators, the following shows an example
of a class whose member tt(sorter) is declared as a friend of the class
tt(Composer):
        verbinclude(//MEMBER examples/concretefriends.cc)
    In this example tt(Friend::d_ints) is a pointer member. It
cannot be a tt(Composer<int>) object as the tt(Composer) class interface
hasn't yet been seen by the compiler when it reads tt(Friend)'s class
interface. Disregarding this and defining a data member tt(Composer<int>
d_ints) results in the compiler generating the error
        hi(field `...' has incomplete type)
        verb(error: field `d_ints' has incomplete type)

`Incomplete type', as the compiler at this points knows of the existence
of the class tt(Composer), but as it hasn't seen tt(Composer)'s interface it
doesn't know what size the tt(d_ints) data member has.
    )