• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Forwarding to mimic multiple inheritance
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Forwarding to mimic multiple inheritance


  • Subject: Re: Forwarding to mimic multiple inheritance
  • From: "John C. Randolph" <email@hidden>
  • Date: Wed, 28 May 2003 13:15:45 -0700

On Wednesday, May 28, 2003, at 06:39 AM, Drew McCormack wrote:

I thought I would try creating a mixin class using message forwarding. I have a CancellableTask mix-in class which has some ivars and implementation, and thus can't be implemented as a protocol.

Umm, protocols don't have implementations. I think you're thinking of a category.

Reading the intro to ObjC, it explains how to mimic multi-inheritance using message forwarding.
If you have B inheriting from A, and you want to mix-in C, you simply implement the method "forwardInvocation:" in B, which is called whenever B receives a message it doesn't recognize, and forward any messages meant for C to an instance C in B. I think you get my drift.

The problem I have with this is a practical one. Should I include the method declarations of the mix-in class C, in B? B doesn't technically implement them, and I will get compiler warnings if I do this. Yet the reason for including a mix-in class in the first place is that in practise B does respond to the methods of C.

Has anyone made use of this trick, and if so, how have they defined the interface in the header files?

Just declare the class that does the forwarding as adopting the protocol in question:

@protocol CancellableTask

- (void) cancel;

@end

@interface CancellableTask : NSObject
{
id someRandomObject;
}

-(void) cancel;

@end

@implementation CancellableTask

-(void) cancel
{
// kill something here
}

@end

@interface Foo : NSObject <CancellableTask>

- forwardInvocation:(NSInvocation *) invocation;

@end

@implementation Foo

- forwardInvocation:(NSInvocation *) invocation
{
// forward to the CancellableTask object here.
}

@end

-jcr


John C. Randolph <email@hidden> (408) 974-8819
Sr. Cocoa Software Engineer,
Apple Worldwide Developer Relations
http://developer.apple.com/cocoa/index.html
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

References: 
 >Forwarding to mimic multiple inheritance (From: Drew McCormack <email@hidden>)

  • Prev by Date: Re: Where in a prefPane can I do some polling, and then update the UI?
  • Next by Date: Re: Attributes in a string
  • Previous by thread: Re: Forwarding to mimic multiple inheritance
  • Next by thread: Interface Builder and projects with variable number of windows
  • Index(es):
    • Date
    • Thread