Initialize a subclass object with a base class object
Initialize a subclass object with a base class object
- Subject: Initialize a subclass object with a base class object
- From: "Liviu Andron" <email@hidden>
- Date: Tue, 20 May 2008 01:28:48 +0300
Problem:
I want to extend NSButtonCell (to keep some additional data) =>
MyButtonCell, but this MyButtonCell objects must be initialized with
some base classes object
@interface MyButtonCell: NSButtonCell {
@private
MyData* fData;
}
+(id)issueWithCell:(NSButtonCell*)cell;
@end
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
I don't want A solution (an workaround), I already have one, but an answer
(to this one or similar problems). I didn't find anything, in C ++ you have
the (base class) copy contructor.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
My first solution looked like this.
+(id)issueWithCell:(NSButtonCell*)cell {
NSParameterAssert(nil != cell);
NSZone* rawMem = [[MyButtonCell alloc] zone];
NSAssert(nil != rawMem, @"MyButtonCel_issueWithCel_6");
if (nil == rawMem)
return nil;
MyButtonCell* issueCell = [MyButtonCell*)[cell copyWithZone:rawMem]
autorelease];
NSAssert(nil != issueCell, @"MyButtonCel_issueWithCel_8");
if (nil == issueCell) {
// \todo how to release the memory
return nil;
}
NSAssert([issueCell isKindOfClass:[MyButtonCell class]],
@"MyButtonCel_issueWithCel_23");
......
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Then I realized:
- the last assertion will fail: issueCell is a NSButtonCell object
- even if this works it's not what I need since it's a "shallow copy"
(copy pointers without retain)
"Re: Overriding -copyWithZone: the right way"
http://lists.apple.com/archives/cocoa-dev/2004/Nov/msg00242.html
"Implementing Object Copy"
http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Other ideas:
- not only derive, but also keep a NSButtonCell member variable
=> how to forward all the requests ??
(I looked at forwardInvocation, but then I found
methodSignatureForSelector :()
- set and retain all the pointers
Ex.
[issueCell SetFont: nil]; [issueCell SetFont: [cell font]];
But:
- do I need to look in the headers and copy everything ? what if
they'll add something new?
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Thanks in advance
_______________________________________________
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