-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy path15.37.txt
32 lines (23 loc) · 1.4 KB
/
15.37.txt
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
Exercise 15.37: What changes would your classes need if the derived classes had
members of type shared_ptr<Query_base> rather than of type Query?
By Faisal Saadatmand
1) The constructor AndQuery, OrQuery and NotQuery parameters' types must be
changed from Query to shared_ptr<Query_base>
2) The parameter(s) and the return type of the overloaded operators, with the
exception of <<, must be change from type Query to shared_ptr<Query_base>
3) All the calls to rep and eval must use the -> access member operator
instead of the "." Member access operator.
4) As a result of 3, the virtual function rep in Query_base must be public,
otherwise it will not be accessible through dereferencing the
shared_ptr<Query_base> within the derived classes. Alternatively, we could have
the derived classes befriend Query_base (but that is awkward syntax). Note:
private or protected access specifier will not work, because dereferencing the
shared_ptr involves derived-to-base conversion, which has its own subtle
restriction on accessibility. (p 613-614
5) The same would apply to eval in Query_base.
6) Query class must define a conversion operator (with a public access
specifier) from Query to shared_ptr<Query_base>
7) Query's constructor that takes a shared_ptr (and the conversion operator)
must be public in order to be accessible to users. This is so for the same
reason mention in 4.
For an implementation, see 15.37a.cpp.