Re: pure virtual methods?
Re: pure virtual methods?
- Subject: Re: pure virtual methods?
- From: publiclook <email@hidden>
- Date: Thu, 24 Jul 2003 18:48:41 -0400
Cocoa'a class clusters work exactly the way you want. They provide
lots of methods that use just a few "primitive" methods. Subclasses
implement just the primitive methods to provide the style of data
storage and implementation wanted and all of the other inherited
methods continue to work powerfully.
Using Cocoa's class clusters as an example, the print a message at
runtime if the base class versions of the primitive methods are ever
called.
This can be accomplished by using the NSAssert macros and/or
-doesNotRecognizeSelector:
doesNotRecognizeSelector:
- (void)doesNotRecognizeSelector:(SEL)aSelector
Handles aSelector messages the receiver doesnt recognize. The runtime
system invokes this method whenever an object receives an aSelector
message it cant respond to or forward. This method, in turn, raises an
NSInvalidArgumentException, and generates an error message.
Any doesNotRecognizeSelector: messages are generally sent only by the
runtime system. However, they can be used in program code to prevent a
method from being inherited. For example, an NSObject subclass might
renounce the copy or init method by reimplementing it to include a
doesNotRecognizeSelector: message as follows:
- copy
{
[self doesNotRecognizeSelector:_cmd];
}
The _cmd variable is a hidden argument passed to every method that is
the current selector; in this example, it identifies the selector for
the copy method. This code prevents instances of the subclass from
responding to copy messages or superclasses from forwarding copy
messagesalthough respondsToSelector: will still report that the
receiver has access to a copy method.
See Selectors for a description of the SEL type.
See Also: forwardInvocation:
On Thursday, July 24, 2003, at 02:28 PM, Francisco Tolmasky wrote:
Ok, in C++ if I made a method virtual =0, then subclasses would have
to define it. I want to do something similar in Objective C. I have
a class that inherits from NSObject but is to be used as a superclass.
It shouldn't be a protocol because it defines and does a lot of the
dirty work in the background, but all subclasses *should* define and
implement a number of methods. How can I ensure this happens without
making a separate protocol that people would have to make subclasses
adhere to?
Francisco Tolmasky
email@hidden
http://users.adelphia.net/~ftolmasky
_______________________________________________
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.
_______________________________________________
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.