Re: is protected broken, or am I?
Re: is protected broken, or am I?
- Subject: Re: is protected broken, or am I?
- From: Brent Gulanowski <email@hidden>
- Date: Thu, 24 Oct 2002 12:57:16 -0400
On Wednesday, October 23, 2002, at 09:08 PM, matt neuburg wrote:
- makeIdenticalTwin {
if ( !twin ) {
twin = [[Sibling alloc] init];
twin->gender = gender;
twin->appearance = appearance;
}
return twin;
}
OKAY, I have to apologize to matt and take back what I said before,
seeing as how I didn't understand what he was getting at or I'd
forgotten something basic. The following code compiles and runs fine:
// main.h
#import <Foundation/Foundation.h>
#import "base.h"
#import "sub.h"
int main (int argc, const char * argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
base *mom = [[base alloc] initWithQuantity:10];
base *dad = [[base alloc] initWithQuantity:6];
sub *billy = [[sub alloc] initWithQuantity:2 unitValue:4.4f];
sub *jane = [[sub alloc] initWithQuantity: 3 unitValue:5.1f];
NSLog(@"mom contains %d", [[mom valueForKey:@"quantity"] intValue] );
NSLog(@"mom and dad contain %d", [dad combinedQuantityWithBase:mom] );
NSLog(@"billy and jane are valued at %f", [billy
combinedValueWithSub:jane] );
NSLog(@"billy and jane are worth %f", [billy totalValueWithSub:jane] );
[mom release]; [dad release]; [billy release]; [jane release];
[pool release];
return 0;
}
// baseClass.h ---------------------- //
#import <Foundation/Foundation.h>
@interface base : NSObject {
int quantity;
}
-(base *)initWithQuantity:(int)q;
-(int)combinedQuantityWithBase:(base *)sibling;
@end
// baseClass.m ---------------------- //
#import "base.h"
@implementation base
-(base *)initWithQuantity:(int)q {
if( (self = [super init]) ) { quantity = q; }
else { [self release]; self = nil; }
return self;
}
-(int)combinedQuantityWithBase:(base *)sibling { return quantity +
sibling->quantity; }
@end
// sub.h ---------------------- //
#import <Foundation/Foundation.h>
#import "base.h"
@interface sub : base {
@private float unitValue; // note private!
}
-(sub *)initWithQuantity:(int)q unitValue:(float)val;
-(float)combinedValueWithSub:(sub *)sibling;
-(float)totalValue;
-(float)totalValueWithSub:(sub *)sibling;
@end
// sub.m ---------------------- //
#import "sub.h"
@implementation sub
-(sub *)initWithQuantity:(int)q unitValue:(float)val {
if( (self = [super initWithQuantity:q]) ) { unitValue = val; }
else { [self release]; self = nil; }
return self;
}
-(float)combinedValueWithSub:(sub *)sibling { return unitValue +
sibling->unitValue; }
-(float)totalValue { return quantity * unitValue; }
-(float)totalValueWithSub:(sub *)sibling {
return (quantity+sibling->quantity)*(unitValue+sibling->unitValue);
}
@end
But here is your original problem statement
(from
http://lists.apple.com/archives/cocoa-dev/2002/Oct/10/
isprotectedbrokenorami.003.txt)
Let me step back a bit and explain the problem. I have a window.
Sometimes when this window appears it behaves a certain way. So the
window is in a nib, and the behavior is encoded into an
NSWindowController subclass, MyClass, that is the nib's owner. The
MyClass instance (File's Owner) and the window are amply endowed with
connectors back and forth.
But on other occasions when this window appears it behaves quite a
different way. I don't want to put the code for this alternate
behavior into MyClass. I'd like to put it into some other class and
have an instance of that other class step into MyClass's coat, as it
were, taking on all the outlets, and being pointed to by all the
window stuff, that the MyClass instance already is set up with in the
nib.
This is why I thought of a subclass of MyClass. If there is a better
architecture for accomplishing this, I'd be glad to hear it.
This is where some of us diverged into a seemingly nonsensical
intepretation of scripture, which I have hopefully now put to rest.
You also wrote:
The problem is that MySubClass does not, as I was hoping, inherit
access to MyClass's ivars. So now I'm asking whether there's a better
way to do this. m.
Which I think I have just shown (in the totalValueWithSub: method
above) is not the case. The Subclass method directly accesses a
superclass ivar for a sibling instance. So now I don't know why you had
the problem in the first place, unless of course you subclassed an
AppKit or Foundation object and tried to access private ivars, which
isn't allowed in subclass methods at all.
Is this issue any clearer than it was?
--
Brent Gulanowski email@hidden
http://inkubator.idevgames.com/
Working together to make great software.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.