See the text, the middle of page 220.
Most answers weren't even close to being right, although some were plausible if you forget what the question was supposed to be.
The virtual flip-flop occurs because while either the dynamic or static type of a instance reference or pointer determines the kind of member-function look-up used, only the static type determines which form of look-up is used.
As an example, given
struct Parent void munge() { ... } struct Child : Parent virtual void munge() { ... } int main() Parent * pp = new Child pp->munge()
main()
calls Parent::munge
because the static type of pp
is
Parent, and in Parent munge()
is non-virtual.
Many answers to this questions had examples that didn't contain the virtual
keyword; as you might expect, those answers weren't successful. A few answers
used virtual class
, which isn't even correct syntax.
template < T > class Handle { public: // blah blah blah private: T * ptr; size_t * ref_cnt; };
|
with |
template < T > class Handle { public: // blah blah blah private: T * ptr; size_t ref_cnt; };
|
What do you think of your colleague's scheme?
Your colleague's scheme is disastrous. The point behind Koenig and Moo's scheme is that the reference count for an object instance being handled can be simply managed in one place but yet still be available to all handles referencing the object.
By moving the reference count into each handle, your colleague has made it much more difficult and expensive to maintain the reference count; now each handle has to be tracked down so it's reference count can be changed. In fact, without extra bookkeeping information, it's impossible to maintain the reference count.
Most answers that weren't wrong managed to dither around not being completely wrong, but few answers indicated exactly what is the problem with eliminating pointers to the reference count.
The example from the assignment does the job:
1: Computer 2 is flaky. 2: Computer 1 is faulty. 1: Computer 2 is faulty.
See the assignment page for an explanation.
Not suprisingly, most answers to this problem were correct.
This page last modified on 12 December 2003.