X-Git-Url: https://git.distorted.org.uk/~mdw/sod/blobdiff_plain/3a774b55edfea441c1715994924c2999e9202143..6afec9101d5ea87e3df4bda2239ffd05f8154fa6:/doc/intro.tex diff --git a/doc/intro.tex b/doc/intro.tex index f4b3168..67a220f 100644 --- a/doc/intro.tex +++ b/doc/intro.tex @@ -88,7 +88,7 @@ are subclasses; \Cplusplus\ has no such root class. Both systems provide multiple inheritance, but go about it very differently. The most important difference is that \Cplusplus\ provides only \emph{static delegation}: if you have a class @|B| which defines some (virtual) member -function @|f|, and a derived class of @|D| which wants to \emph{extend} the +function @|f|, and a derived class @|D| which wants to \emph{extend} the behaviour of @|f| on instances of @|D|, then you must explicitly call @|B::f| at the appropriate point: \begin{prog} @@ -143,10 +143,13 @@ which is unlikely to be what was wanted: `B' prints twice, and the `before' and `after' actions are both in the middle.\footnote{% Of course, one could have arranged to call @|Y::f| before @|X::f| -- but the important point is that one would have needed to \emph{know} that this - was necessary.} % + was necessary. And you still end up with two copies of `B'.} % The problem is that correctly composing behaviour from a collection of superclasses requires knowledge of all of the superclasses involved and how -they're supposed to work together. +they're supposed to work together. Messing with virtual base classes has +eliminated the problem of duplicating @|B|'s state, but has done nothing to +help avoid duplicating @|B|'s \emph{behaviour} -- which is a shame, because +duplicating one without the other is going to end badly. The obvious workaround is to separate the functionality -- here, printing the messages -- from the plumbing, which arranges to do everything in the right @@ -161,7 +164,7 @@ the various @|_f| functions in the right order, like this: void _f() \{ std::cout <{}< "B@\\n"; \} \-\\ public: \\ \ind virtual void f() \{ _f(); \} \\ - virtual ~B() \{ \} \-\\ + virtual @~B() \{ \} \-\\ \}; \\+ % class X: virtual public B \{ \\