Historia wymaga pasterzy, nie rzeźników.

..
};
Such a handle can be passed around freely. For example:
The C++ Programming Language, Third Edition by Bjarne Stroustrup. Copyright ©1997 by AT&T.
Published by Addison Wesley Longman, Inc. ISBN 0-201-88954-4. All rights reserved.
784
Roles of Classes
Chapter 25
v
vo
oi
id
d f
f1
1( H
Ha
an
nd
dl
le
e< S
Se
et
t>)
;
H
Ha
an
nd
dl
le
e< S
Se
et
t> f
f2
2()
{
H
Ha
an
nd
dl
le
e< S
Se
et
t> h
h( n
ne
ew
w L
Li
is
st
t _
_ s
se
et
t< i
in
nt
t>)
;
/
/ ...
r
re
et
tu
ur
rn
n h
h;
}
v
vo
oi
id
d g
g()
{
H
Ha
an
nd
dl
le
e< S
Se
et
t> h
hh
h = f
f2
2()
;
f
f1
1( h
hh
h)
;
/
/ ...
}
Here, the set created in f
f2
2() will be deleted upon exit from g
g() – unless f
f1
1() held on to a copy;
the programmer does not need to know.
Naturally, this convenience comes at a cost, but for many applications the cost of storing and maintaining the use count is acceptable.
Sometimes, it is useful to extract the representation pointer from a handle and use it directly.
For example, this would be needed to pass an object to a function that does not know about handles. This works nicely provided the called function does not destroy the object passed to it or store a pointer to it for use after returning to its caller. An operation for rebinding a handle to a new representation can also be useful:
t
te
em
mp
pl
la
at
te
e< c
cl
la
as
ss
s X
X> c
cl
la
as
ss
s H
Ha
an
nd
dl
le
e {
/
/ ...
X
X* g
ge
et
t _
_ r
re
ep
p()
{ r
re
et
tu
ur
rn
n r
re
ep
p;
}
v
vo
oi
id
d b
bi
in
nd
d( X
X* p
pp
p)
{
i
if
f ( p
pp
p != r
re
ep
p)
{
i
if
f (--* p
pc
co
ou
un
nt
t == 0
0)
{
d
de
el
le
et
te
e r
re
ep
p;
* p
pc
co
ou
un
nt
t = 1
1; /
/ recycle pcount
}
e
el
ls
se
e
p
pc
co
ou
un
nt
t = n
ne
ew
w i
in
nt
t( 1
1)
; /
/ new pcount
r
re
ep
p = p
pp
p;
}
}
};
Note that derivation of new classes from H
Ha
an
nd
dl
le
e isn’t particularly useful. It is a concrete type
without virtual functions. The idea is to have one handle class for a family of classes defined by a base class. Derivation from this base class can be a powerful technique. It applies to node classes as well as to abstract types.
The C++ Programming Language, Third Edition by Bjarne Stroustrup. Copyright ©1997 by AT&T.
Published by Addison Wesley Longman, Inc. ISBN 0-201-88954-4. All rights reserved.
Section 25.7
Handle Classes
785
As written, H
Ha
an
nd
dl
le
e doesn’t deal with inheritance. To get a class that acts like a genuine use-counted pointer, H
Ha
an
nd
dl
le
e needs to be combined with P
Pt
tr
r from §13.6.3.1 (see §25.10[2]).
A handle that provides an interface that is close to identical to the class for which it is a handle is often called a proxy. This is particularly common for handles that refer to an object on a remote machine.
25.7.1 Operations in Handles [role.handle.op]
Overloading -> enables a handle to gain control and do some work on each access to an object.
For example, one could collect statistics about the number of uses of the object accessed through a handle:
t
te
em
mp
pl
la
at
te
e < c
cl
la
as
ss
s T
T> c
cl
la
as
ss
s X
Xh
ha
an
nd
dl
le
e {
T
T* r
re
ep
p;
i
in
nt
t n
no
o _
_ o
of
f _
_ a
ac
cc
ce
es
ss
se
es
s;
p
pu
ub
bl
li
ic
c:
T
T* o
op
pe
er
ra
at
to
or
r->()
{ n
no
o _
_ o
of
f _
_ a
ac
cc
ce
es
ss
se
es
s++; r
re
et
tu
ur
rn
n r
re
ep
p;
}
/
/ ...
};