• 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: Inheritance question
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Inheritance question


  • Subject: Re: Inheritance question
  • From: Chris Suter <email@hidden>
  • Date: Fri, 17 Aug 2007 21:16:13 +1000


On 17/08/2007, at 9:02 PM, Tony Wroblewski wrote:

It doesn't work because you are still sending the message to your object, even if you cast it. It doesn't work the same as C++, when you can do virtual methods.

Just to add to this, objective C resolves method calls at run-time.

Wouldn't [[[myClassInstance] super] theMethod] work, although its incredibly ugly

No, it won't.

The only way I can think of doing it would be to call objc_msgSendSuper directly.

For example, this appears to work:

#include <Foundation/Foundation.h>
#include <objc/objc-runtime.h>

@interface ObjectA : NSObject {
}

@end

@implementation ObjectA

- (void)myMethod
{
  NSLog (@"ObjectA");
}

@end

@interface ObjectB : ObjectA {
}

@end

@implementation ObjectB

- (void)myMethod
{
  NSLog (@"ObjectB");
}

@end

int main ()
{
  ObjectB *b = [[ObjectB alloc] init];

  struct objc_super s = { b, [ObjectA class] };

  objc_msgSendSuper (&s, @selector (myMethod));

  return 0;
}

Having said that, I can't think of any good reason why you'd want to do this. If you want to expose functionality in a super class write another method.

- Chris



Attachment: smime.p7s
Description: S/MIME cryptographic signature

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

References: 
 >Re: Inheritance question (From: "Daniel Angermeier" <email@hidden>)
 >Re: Inheritance question (From: Tony Wroblewski <email@hidden>)

  • Prev by Date: Re: Inheritance question
  • Next by Date: Re: When does an object own its instance variable?
  • Previous by thread: Re: Inheritance question
  • Next by thread: Re: Inheritance question
  • Index(es):
    • Date
    • Thread