What is the equivalent of a C++ pure-virtual function in Objective-C?
-
I want to force derived classes to implement a given interface without
provided a default implementation. Does the concept exist in
Objective-C (I'm almost sure it does)? If so, what does the syntax
look like?
-Michael
----------------------
The united stand. The divided get played.
-- Bernie MAC -
They're called "Protocols" and were the original inspiration for
Java's interfaces:
http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/Articl
es/chapter_7_section_1.html
HTH,
Dave
On 11 Jan, 2009, at 7:20 PM, Michael A. Crawford wrote:> I want to force derived classes to implement a given interface
> without provided a default implementation. Does the concept exist
> in Objective-C (I'm almost sure it does)? If so, what does the
> syntax look like?
>
> -Michael -
I submitted this question only after trying to find the answer in a
couple newer Cocoa programming texts that I own. Then, I went and dug
up a older text from 2002: Cocoa Programming from Anguish, Buck, and
Yacktman. I found something called a Protocol. From what I've read
so far this is what I need. I jumped the gun on posting the question
here before exhausting my local resources. Sorry for that. Still,
anyone wants to give me some guidance on protocols and how they should
be employed, I'm all ears (eyes).
-Michael
----------------------
The difference between genius and stupidity...
...is that genius has its limits."
-- Albert Einstein
On Jan 11, 2009, at 6:20 PM, Michael A. Crawford wrote:> I want to force derived classes to implement a given interface
> without provided a default implementation. Does the concept exist
> in Objective-C (I'm almost sure it does)? If so, what does the
> syntax look like?
>
> -Michael
> ----------------------
> The united stand. The divided get played.
>
> -- Bernie MAC -
On 12 Jan 2009, at 1:20 pm, Michael A. Crawford wrote:> I want to force derived classes to implement a given interface
> without provided a default implementation. Does the concept exist
> in Objective-C (I'm almost sure it does)? If so, what does the
> syntax look like?
The nearest thing to pure virtual methods is a formal protocol,
declared using the @protocol directive.
file:///Developer/Documentation/DocSets/com.apple.ADC_Reference_Library.CoreReference.docset/Contents/Resources/Documents/documentation/Cocoa/Conceptual/ObjectiveC/Articles/chapter_7_section_6.html
A class that "conforms to" the formal protocol is required to
implement the methods of the protocol.
--Graham -
On 11-Jan-09, at 6:32 PM, <cocoa-dev-request...> wrote:> Still,
> anyone wants to give me some guidance on protocols and how they should
> be employed, I'm all ears (eyes).
Well, here's one I just employed. Define it like this
@protocol PAImageCollection <NSObject>
@required
- (int)collectionCount;
- (UINavigationController *)collectionController;
- (NSString *)fileForIndex:(int)collectionIndex;
- (BOOL)canRemove;
@end
and then any class you declare like this
@interface FavoritesTableViewCell : UITableViewCell <PAImageCollection>
will throw up a compile error if it doesn't match everything under
@required in the protocol.
And to keep track of a derived instance as a variable/argument,
something like
id<PAImageCollection> collection;
does the trick.
That should pretty much cover any sane use of pure virtual functions
you have in mind.
--
Alex Curylo -- <alex...> -- http://www.alexcurylo.com/
"I misjudged you Alex; you're a @#$%!!, but you're a consistent,
smart $#@%^!!, and the kind of @$^#&$!! I can #$@*&!! respect."
-- John G. Spragge -
Le 12 janv. 09 à 03:29, Graham Cox a écrit :>
> On 12 Jan 2009, at 1:20 pm, Michael A. Crawford wrote:
>
>> I want to force derived classes to implement a given interface
>> without provided a default implementation. Does the concept exist
>> in Objective-C (I'm almost sure it does)? If so, what does the
>> syntax look like?
>
>
> The nearest thing to pure virtual methods is a formal protocol,
> declared using the @protocol directive.
>
> file:///Developer/Documentation/DocSets/com.apple.ADC_Reference_Library.CoreReference.docset/Contents/Resources/Documents/documentation/Cocoa/Conceptual/ObjectiveC/Articles/chapter_7_section_6.html
>
> A class that "conforms to" the formal protocol is required to
> implement the methods of the protocol.
Just for the record. Mac OS 10.5 introduced two new keywords to define
methods in a protocol:
@required
@optional.
A class that "conforms to" the formal protocol is required to
implement the methods marked as required of the protocol. -
On 12 Jan 2009, at 7:33 pm, Jean-Daniel Dupas wrote:> Just for the record. Mac OS 10.5 introduced two new keywords to
> define methods in a protocol:
>
> @required
> @optional.
>
> A class that "conforms to" the formal protocol is required to
> implement the methods marked as required of the protocol.
Yes, but if you don't specify anything, @required is assumed.
--Graham


