Re: Abstract classes and methods
Re: Abstract classes and methods
- Subject: Re: Abstract classes and methods
- From: "Michael B. Johnson" <email@hidden>
- Date: Wed, 29 Aug 2001 11:06:47 -0700
- Organization: Pixar Animation Studios
Ondra Cada wrote:
>
>
Michael,
>
>
>>>>>> Michael B. Johnson (MBJ) wrote at Wed, 29 Aug 2001 08:12:58 -0700:
>
MBJ> The way I did this exact thing in WavesWorld way back when was by having
>
MBJ> a formal "Renderable" protocol - any object that implemented the various
>
MBJ> methods defined in the protocol was, by definition, renderable. This let
>
MBJ> me mix things like cameras and geometry that didn't have a common base
>
MBJ> class in a very nice way, where in C++ I would have defined an abstract
>
MBJ> base class with all pure virtual functions and then done a mix in with
>
MBJ> the appropriate concrete superclass.
>
>
Right. The only drawback is that you can't have *any* implementation in
>
protocol, not even something utterly type-independent like
>
>
-(int)rightmost { return [self leftmost]+[self width]; }
>
>
(don't please beat me over head by NSRect, I hope you see the point)
>
>
which might get quite handy on ocassions, and which is the only thing
>
protocols do lack when compared with multiple inheritance.
>
I completely agree, especially when you compare it to the fact that in C++ you can define abstract
base classes with all pure virtual functions, and then go ahead and implement all of them with some
useful stub. This was very useful when doing things like having a method like writeToRIB() where
you might want to require that every subclass implement it, but you want to let it default to
writing out something like:
# the class WWTorus has not implemented writeToRIB yet...
to the file.
this was easy to do in C++, but in Objective-C, I came up with a concrete subclass that conformed to
the protocol, and then had subclasses that used a common implementation of certain of the routines
inherit from that. This was arguably easier to do with multiple inheritance in C++ than with
protocols in Objective-C.
--> Michael B. Johnson, Ph.D. -- email@hidden
--> Studio Tools, Pixar Animation Studios
-->
http://xenia.media.mit.edu/~wave