Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't the super class of a meta class of a root class a NULL pointer?

In the objective-c runtime, a class is represented by a structure like this:

typedef struct class_t {
    struct class_t *isa;    // Points to the class' meta class
    struct class_t *super;  // Points to the class' super class
    Cache cache;
    IMP *vtable;
    union {
        class_ro_t *rodata;
        class_rw_t *rwdata;
    };
} class_t;

When a MyClass class is a root class and not a meta class, its super member is a NULL pointer (which is understandable, since it is a root class, so it doesn't have a super class). However, if we get the isa member of that same MyClass root class (which points to MyClass' meta class), and we get the super member of that isa member, it is not a NULL pointer, but a pointer to MyClass (not meta class). Shouldn't the super class of a meta class of a non-meta root class be a NULL pointer (as the super class of a non-meta root class is)? Since it's not, for what is it supposed not to be?

I'm asking this because, the way things are actually, if methodX is an instance method (not a class method) of a MyClass root class, the statement [MyClass methodX] would call methodX (and it actually does), what shouldn't happen (again, because methodX is a instance method, not a class method, so it should not respond to the [MyClass methodX] statement).

like image 815
LuisABOL Avatar asked Jan 15 '23 20:01

LuisABOL


1 Answers

The Obective-C language specifically decided that class objects should respond to the instance methods of their root class. This is primarily a pragmatic decision; since NSObject is mostly responsible for handling the fundamentals of how ObjC interacts with objects, it would be annoying to have to implement everything twice; once for class objects and once for instances. Class objects can be put into arrays, etc. so they need to respond to -retain and -release just like instances do.

You can read more about it in the "Methods of the Root Class" section of this document: https://developer.apple.com/library/ios/DOCUMENTATION/Cocoa/Conceptual/ObjectiveC/Chapters/ocObjectsClasses.html

like image 74
BJ Homer Avatar answered Jan 18 '23 22:01

BJ Homer