Do different instances of an object share the same pointer to a method in memory

StackOverflow https://stackoverflow.com/questions/20668277

  •  19-09-2022
  •  | 
  •  

문제

If I created two objects and executed the same method on those two objects, is the pointer for those two methods of different instantiations of the same object type, the same.

Example:

NSObject *obj1 = [NSObject new];
NSObject *obj2 = [NSObject new];

[obj1 doSomething];
[obj2 doSomething];

Is the pointer to doSomething the same for both of those objects. How could I prove it is or it isn't? I've been told it is.

도움이 되었습니까?

해결책

It is - there would be no point in creating a new function pointer for each object. (And it would also be ridiculously hard to do so.)

You can observe this by e. g. reading the NSObject class reference. This class has a method called instanceMethodForSelector:. This is a class method, so it can only return a function pointer which is not dependent on instances, only on the class (and the selector, of course).

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top