back to listing index

In OOP, what is forwarding and how is it different from delegation?

[web search]
Original source (stackoverflow.com)
Tags: delegation oop design-patterns forwarding inheritance
Clipped on: 2016-08-09

Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

I want to help →

Would someone please explain the difference between forwarding and delegation? They seem similar, but I haven't been able to find a good definition of forwarding, so I'm not sure I really understand.

asked Oct 19 '11 at 2:45
Image (Asset 3/5) alt=
vette982
1,29862138
   upvote
  flag
Not sure why this got voted down... It seems like a useful question, although it could use a bit more elaboration. Unfortunately, we could be in a Catch-22 here. I'll try to provide an answer, and the questioner can tell me if I understand correctly. – Michael Price Oct 19 '11 at 2:50

Forwarding is sort of like "inheritance via containment", or "implementation inheritance the hard way".

Typical implementation inheritance:

class Base
{
 public:
    void baseFn() { }
};

class Derived : public Base
{
 public:
    void derivedFn() { }
};

Now, an instance of Derived has a baseFn() method. This is a way of sharing implementation between different classes.

Forwarding looks like this:

class Contained
{
 public:
    void containedFn() { }
};

class Thing
{
 public:
    void thingFn() { }
    void containedFn() { mContained.containedFn(); }
 private:
    Contained mContained;
};

You could have also implemented that with private inheritance.

Delegation is a special case of forwarding, where at the "thing to forward" to is an interface itself.

class Delegate
{
 public:
    virtual void doDelegateAction() = 0;
};

class DelegateA : public Delegate
{
    virtual void doDelegateAction() { }
};

class DelegateB : public Delegate
{
    virtual void doDelegateAction() { }
};

class Thing
{
 public:
    void Thing (Delegate * delegate) { mDelegate = delegate; }
    void thingFn() { }
    void containedFn() { if (mDelegate) mDelegate->doDelegateAction(); }
 private:
    Delegate * mDelegate; // Note, we don't own this memory, buyer beware.
};

Now, you can swap out the implementation of delegate at runtime, whereas in forwarding you cannot (and you may not want to, which is why you would do it).

If that answers the wrong question, let me know in a comment and I'll remove the answer.

answered Oct 19 '11 at 3:01
Image (Asset 4/5) alt=
Michael Price
2,8611720
   upvote
  flag
I don't think unique_ptr is copyable. – Kerrek SB Oct 19 '11 at 3:05
   upvote
  flag
So are you saying forwarding requires the class to have the delegated object contained within the class, but delegation can optionally use objects instantiated outside of the class? The distinction between the two is still not completely clear. – vette982 Oct 19 '11 at 4:44
   upvote
  flag
@vette982 Sorry, I left out a key peice in my last example... Edit coming shortly. – Michael Price Oct 19 '11 at 16:35
   upvote
  flag
@KerrekSB Ah, you are correct. I'm changing the example to & since that is simpler. – Michael Price Oct 19 '11 at 16:41
   upvote
  flag
@KerrekSB Decided to go with * instead in order to maintain the runtime replacement of the delegate. – Michael Price Oct 19 '11 at 16:43

Let's first define two terms:

  • sender : the object that sends a message/task to another object(the receiver)
  • receiver: the object that receives a message/task from the sender

The difference between forwarding and delegation is that in forwarding the receiver acts in its own context whereas in delegation the receiver acts on the behalf of the sender.

Here is a great metaphor from this blog post:

Delegation and forwarding are both very similar. One metaphor that might help distinguish them is to think of receiving an email asking you to donate some money to a worthy charity.

  • If you forward the email to a friend, and the friend donates money, the friend is donating their own money and getting their own tax receipt.
  • If you delegate responding to your accountant, the accountant donates your money to the charity and you receive the tax receipt.
answered Feb 16 '15 at 10:45
Image (Asset 5/5) alt=
AnthonyS
1,34111215

They're similar ideas in that one object relies on another for help. Here's how I think of the two ideas given my strong Objective-C bias:

delegation: A decision needs to be made, but I don't want to make it. I'll let my delegate handle that.

In Cocoa, for example, NSTableView uses a delegate to customize the behavior of the table. Delegation provides a way to customize one object by letting another object, the delegate, provide the customization. Continuing with the example, a table view's delegate implements an NSTableViewDelegate interface that the table uses to talk to its delegate.

forwarding: Someone just sent me a message that I don't understand, but I know of another object that might implement it. I'll pass the invocation of that message on to that object.

In Cocoa, again, any class can implement the -forwardInvocation: method. If a message is sent to an object that doesn't implement it, that object's -forwardInvocation: method is called, and the object can decide to pass the invocation on to another object. That object could be its delegate, or it could be some system-wide error handler, or whatever. NSProxy uses this to appear to implement all methods -- it just passes the invocation on to its master object.

Note that with forwarding, there's not a defined delegate interface; the message is just passed on to another object. Another place you see what I'd call forwarding is when one object contains another object that it uses to implement some interface. Any messages to that interface are just forwarded to the contained object, which does all the work.

answered Oct 19 '11 at 2:54
Caleb
95.8k13119212

Your Answer

asked

4 years ago

viewed

5265 times

active

1 year ago

Featured on Meta

Hot Network Questions

Technology Life / Arts Culture / Recreation Science Other
  1. Stack Overflow
  2. Server Fault
  3. Super User
  4. Web Applications
  5. Ask Ubuntu
  6. Webmasters
  7. Game Development
  8. TeX - LaTeX
  1. Programmers
  2. Unix & Linux
  3. Ask Different (Apple)
  4. WordPress Development
  5. Geographic Information Systems
  6. Electrical Engineering
  7. Android Enthusiasts
  8. Information Security
  1. Database Administrators
  2. Drupal Answers
  3. SharePoint
  4. User Experience
  5. Mathematica
  6. Salesforce
  7. ExpressionEngine® Answers
  8. more (13)
  1. Photography
  2. Science Fiction & Fantasy
  3. Graphic Design
  4. Movies & TV
  5. Seasoned Advice (cooking)
  6. Home Improvement
  7. Personal Finance & Money
  8. Academia
  9. more (9)
  1. English Language & Usage
  2. Skeptics
  3. Mi Yodeya (Judaism)
  4. Travel
  5. Christianity
  6. Arqade (gaming)
  7. Bicycles
  8. Role-playing Games
  9. more (21)
  1. Mathematics
  2. Cross Validated (stats)
  3. Theoretical Computer Science
  4. Physics
  5. MathOverflow
  6. Chemistry
  7. Biology
  8. more (5)
  1. Stack Apps
  2. Meta Stack Exchange
  3. Area 51
  4. Stack Overflow Careers
site design / logo © 2016 Stack Exchange Inc; user contributions licensed under cc by-sa 3.0 with attribution required
rev 2016.8.8.3869