• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: How to force a message to a overriding method from within an init method
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to force a message to a overriding method from within an init method


  • Subject: Re: How to force a message to a overriding method from within an init method
  • From: Shawn Erickson <email@hidden>
  • Date: Wed, 11 Feb 2009 18:16:28 -0800

On Wed, Feb 11, 2009 at 5:31 PM, João Varela <email@hidden> 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 (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

References: 
 >How to force a message to a overriding method from within an init method (From: João Varela <email@hidden>)

  • Prev by Date: Re: Application Architecture or approach
  • Next by Date: Re: How to force a message to a overriding method from within an init method
  • Previous by thread: How to force a message to a overriding method from within an init method
  • Next by thread: Re: How to force a message to a overriding method from within an init method
  • Index(es):
    • Date
    • Thread