On Wed, Feb 11, 2009 at 5:31 PM, João Varela <j.var...@clix.pt> wrote:
> Hi all
>
> I am porting most of my C++ code into Objective C and at the moment I have
> this problem:
>
> In C++ to force a constructor to call a overriding method of a subclass I
> used pure virtual functions defined in an abstract (super) class.
...
> I know that the closest thing to a pure virtual function in Objective C is a
> formal protocol.

Not really, no need to involve a protocol.

Conceptually, using C++ terminology, you can think of all methods in
Objective-C as virtual. The "pure" aspect (aka abstract) in
Objective-C simply comes about by the abstract base class simply
providing an empty implementation of the abstract method with
documentation in the interface stating that sub-classes must provide
an implementation.

> My question is: can you implement such a behavior in objective C ...?

Quick example written in mail (not compiled or tested... expect typos)

@inteface Foo : NSObject {
        ...
}
/** Document this as being abstract */
- (void) readX:(int*)x y:(int*)y;
@end

@implementation Foo
- (id) init {
        if ((self = [super init]) != nil) {
                int x, y;
                [self read:&x y:&y];
                ...
        }
        return self;
}
- (void) readX:(int*)x y:(int*)y {
        // the following is optional, use if you want to flag
subclasses that don't override this method
        [self doesNotRecognizeSelector:_cmd];
}
@end

@interface Bar : Foo {
        ...
}
...
@end

@implementation Bar
- (id) init {
        if ((self = [super init]) != nil) {
                ...
        }
        return self;
}
- (void) readX:(int*)x y:(int*)y {
        ... real implementation ...
}
@end

> Related to this, what method gets called inside the init method of the
> superclass: a) the superclass method? or b) the overriding method if there
> is one?

The later (B).

-Shawn
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to