TITLE: delegation (Newsgroups: comp.lang.c++.moderated, 9 Jan 97) RMARTIN: com!rmartin@uunet.uu.net says... Delegation is simply containment and forwarding. SURESH: sureshv@datagenics.com How about Delegation is private inheritance? APPLETON: bradapp@enteract.com (Brad Appleton) That would be incorrect. Strictly speaking, delegation is the ability of one object to forward messages and/or the responsibility for servicing one or more messages to another object. The other object does not have to be a component, or superclass portion of the forwarding object; it could an object associated via some other means, or even a global object of some kind (perish the thought ;-). [ I would also add the important definition that all method invocations and data accessing is done in the context of the delegator, not the delegatee. For example, class Delegator { public: ...; void foo () { fDelegatee->foo (); } void bar () { cout << "Delegator::bar"; } private: Delegatee* fDelegatee; }; class Delegatee { public: ...; void foo () { /* fDelegator-> */ bar(); } void bar () { cout << "Delegatee::bar"; } private: /* Delegator* fDelegator; */ }; If C++ had delegation, the result of calling "foo" on the delegator would result in "Delegator::bar" being output. Since C++ does not directly support this, you as designer, would have to uncomment the code to manually make sure that bar() is invoked in the context of the delegator. - adc ] Inheritance is actually a special form of delegation: a subclass delegates to its superclass, the responsibility for servicing all messages that it accepts but does not implement. Such delegation does not restrict the delegate to be only a private superclass It could just as easily be a public one, where the method that does the delegation has a different name and/or signature from the method of the delegate the is ultimately invoked to fulfill the request. The concept of delegation is orthogonal to the concept of class. It is possible for a language to have objects + delegation but no classes (Self is such a langauge). It is also possible for a language to have objects and classes, but no direct support for delegation (which means no inheritance of method implementations among other things, although you could still inherit data). Languages like C++ do not have direct support for delegation (other than method-by-method): You have to explicitly code up all message forwarding calls (as in Robert's example) like: Foo::bar() { myItem->bar(); } This type of delegation is often referred to as "propagation". There are other forms however. For example, it would be nice if you could tell an object to forward (or delegate) all messages that it doesnt understand to some other object (like one of its components). This would be very useful for implementing proxies, surrogates, and wrappers (perhaps adding some additional pre and/or post invocation code). If this were added to C++, the syntax might perhaps look something like a switch statement by adding reserved words "delegate" and "method" as in: class MyDelegator { private: MyDelegate delegate1, *delegate2; // ... the usual stuff here // Now specify delegation details using method names // and/or method signatures to be forwarded. delegate { method foo : // all methods named foo() regardless of the args method bar(int) : delegate1.method; // <- invokes the method of delegate1 with break; // the same signature and arguments as the // method being delegated default : // delegate everything else that we dont understand // to delegate2 delegate2->lock(); // do some pre-delegation code delegate2->method; // forward the message delegate2->unlock(); // do some post-delegation code }; } PLEASE NOTE that I am *not* proposing the above syntax be added to C++, or even that delegation be added as a language feature. This is just an example whipped up for demonstrative purposes (and probably not even a very good one at that)! Perl happens to let you do this form of delegation via its AUTOLOAD mechanism (which uses completely different syntax from the example given above).