Re: How to force a message to a overriding method from within an init method
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: Erik Buck <email@hidden>
- Date: Wed, 11 Feb 2009 21:28:02 -0500
Calling a subclass's overridden implementation of a superclass member
function from within the super class constructor is very very
dangerous in C++. I don't believe if is even supported by the ANSI/
ISO standard, and to the extent it works at all, it is probably
compiler and linker dependent. I could be wrong of course. This may
be one of the more obscure border cases in the world's most complex
language.
In Objective-C, the subclass's override of the base class method will
be be called polymorphicly because there is no such thing as a
"constructor" in Objective-C. Methods like +alloc and -init are just
regular methods.
#import <Foundation/Foundation.h>
@interface Base : NSObject
{
}
- (void)printName;
@end
@implementation Base
- (id)init
{
self = [super init];
[self printName];
return self;
}
- (void)printName
{
printf("I am the base class implementation.\n");
}
@end
@interface Subclass : Base
{
}
@end
@implementation Subclass
- (void)printName
{
printf("I am the subclass implementation.\n");
}
@end
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Subclass *sample = [[Subclass alloc] init];
[sample release];
[pool drain];
return 0;
}
Output will be
[Session started at 2009-02-11 21:27:00 -0500.]
I am the subclass implementation.
The Debugger has exited with status 0.
_______________________________________________
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