Re: Inheritance question
Re: Inheritance question
- Subject: Re: Inheritance question
- From: Gregory Weston <email@hidden>
- Date: Fri, 17 Aug 2007 10:12:32 -0400
Daniel Angermeier wrote:
In the context of MyClass:
[(MySuperClass *)self theMethod];
However, this call does not invoke MySuperClass's implementation but
that of MyClass.
How can force it to use the method in MySuperClass ?
[super theMethod];
Ok, this would work for that particular case, but what if I have an
instance of MyClass and want to call a super method on it.
[(MySuperClass *)myClassInstance theMethod];
In all honesty, I think the correct solution to that situation is to
rethink your design. You've started to pay too much attention to
implementation rather than protocol. What's the circumstance you're
in where you've decided that you need to specialize but sometimes you
don't want to invoke that specialization?
why does this casting approach at compile time not work ?
Because it's not C++ and static_cast<>. The semantics of the [obj
msg] construct are that you are sending a message 'msg' to the object
'obj', and 'obj' is expected to either reject or handle the message
in the way that its class defines.
Read that again. The concept is: "Hey, obj. Please respond to msg."
It is not: "Hey, msg. Please operate on obj."
There's really no syntax for "Please respond to this message,
pretending that you're an instance of the superclass of your actual
class."
Is there any other way to force the run time to use the super
implementation of that method ?
Not without relying on implementation details. Your supportable
options are:
Have the subclass respond to the message superFoo with a method
consisting of "[super foo];".
Don't actually override foo in the subclass. Change the message to
something that identifies the distinction.
Rework the design so you care about the interface rather than the
implementation.
_______________________________________________
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