views:

62

answers:

2

Hi,

Is it somehow possible to choose the super of a class (preferably in the alloc or init method) so my class inherits from something else?

+3  A: 

You can do that in -init by creating an instance of the desired target class and resetting self. Be sure to send a -release message to the previous instance if you do that, though.

jlehr
A: 

No, you can’t. And: yes, you can.

A class(!) is a relative static thing. It is “hard wired” to its superclass. This is important as there must be some thing, knowing how to handle the allocated memory and other things.

Something different is an instance. You get an instance of some class by “asking” its class for one. (And, usually, after getting one you ask for initialization and so on.)

At this point the usual behaviour can be “broken”. But, as I think, this is nothing to do for beginners, you should be experienced when doing such tings! Why? Read the stuff, Apple is saying about this. And see, that even Apple does such things! Have a look at class clusters like NSNumber: http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaObjects/CocoaObjects.html#//apple_ref/doc/uid/TP40002974-CH4-SW34 (Can’t post more than this one, but you surely find NSNumber-documentation without my help.)

Objective Interested Person