Late snap versus dynamic snap

I read everywhere that Objective-C has true dynamic binding, where C ++ has only late-time binding. Unfortunately, not one of the books explicitly explains this or discusses the underlying implementation. For example, for example, C ++ uses a virtual table. What about Objective-C?

+3
source share
2 answers

http://www.gnu.org/software/gnustep/resources/ObjCFun.html has a pretty good description.

Basically, that such a dynamic binding means that at the moment when the method call is actually made, a decision is made about which method to call. And this method can, if you want, be dynamically selected at this point.

: , . , , . Objective C , isa . , :

struct objc_class {
    Class isa;
    Class super_class;
    const char *name;
    long version;
    long info;
    long instance_size;
    struct objc_ivar_list *ivars;
    struct objc_method_list **methodLists;
    struct objc_cache *cache;
    struct objc_protocol_list *protocols;
};

, :

Follow isa to find the class
if implementation = class.lookup_method(method):
    call implementation
else if get_implementation = class.lookup_method(forwardInvocation):
    implementation = get_implementation(method)
    if implementation:
        call implementation
    else:
       raise runtime error
else:
    raise runtime error

lookup_method?

def lookup_method (class, method):
    if method in class.objc_cache:
        return implementation from objc_cache
    else if method in class.objc_method_list:
        cache implementation from objc_method_list
        return implementation
    else if implementation = class.super_class.lookup_method(method):
        cache implementation
        return implementation
    else:
        return null

, , ++. , 1/3 . Objective C , .

, C. , forwardInvocation, , . , , . - , - . .

, ++ , , .

, , ?

+7

, . , , ( , ..), v ( ). , v.

0

All Articles