Why casting doesn't change the isa pointer
id a = self->isa;
id b = ((NSObject*)self)->isa;
a and b have the same meaning.
Now the given isa pointer is the same why
[(Animal*)Person speakEnglish];
gives an error? I thought the casting would change the isa pointer because if I pass the child class to the parent class I can no longer call the child method and the invoke search method is based on the isa pointer.
I'm curious to know how the method selector is implemented so that even the isa pointer points to Person. If I call Person method on an instance, I get an error.
source to share
I assume it Animal
doesn't have a method -speakEnglish
, but the original class Person
does.
Then you get a compiler error because the method is not declared. Note that (as @jrturton and @ xlc0212 mentioned) the compiler doesn't know anything about runtime.
It is possible that the object (at runtime) has a selector that can be called:
[(Animal*)Person performSelector: @selector(speakEnglish)];
This uses runtime data. More precisely, it checks to see if a method implementation is associated with this selector and calls that implementation.
However, the compiler doesn't know as the compiler uses static type information. Thus, you must provide the correct type information. But you are doing the opposite: you are throwing out the correct type.
source to share