The question is generally coming from self = [super init]
.
In case if I'm subclassing NSSomething and in my init's method self = [super init]
returns object of different class, does it mean I am not able to have my very own ivars in my subclass, just because self
will be pointing to different class?
Appreciate if you could bring some examples if my statement is wrong.
UPD: to make my question more precise.
@implementation Base
-(id) init
{
return [NSNumber numberWithBool:YES];
}
@end
@interface Child : Base
{
int myVeryOwnVar;
}
- (id) init;
- (void) dump;
@end
@implementation Child
- (id) init
{
self = [super init];
myVeryOwnVar = 5;
return self;
}
@end
Child *p = [[Child alloc] init];
[p dump];
This obviously crashed on sending message to p
object, since it's now NSNumber
and does not serve dump message. So I lost the control over myVeryOwnVar and in worse cases can have leaks and crashes.